簡體   English   中英

R-每兩行相加,然后除以該行的第一行

[英]R - sum every two rows and divide by the first row in that sum

我有一個數據框如下

      a          b
24    11.67     -1
39     8.14      1
42     8.12      1
90    10.50     -1
137   13.53     -1
405   47.45      1
416   58.11     -1
454   54.13      1
467   47.82      1
500   59.31     -1
508   61.18     -1
598   51.67      1
626   49.86      1
663   58.47     -1
677   64.85     -1
919   91.15      1
926   82.79      1
974  111.51     -1
1024  85.33      1
1103 118.79     -1

所以在這種情況下,我想要的是以下方式的列表:

(11.67*-1+8.14*1)/11.67
(8.12*1+10.50*-1)/8.12
(13.52*-1+47.45*1)/13.53
.
.
.
that is --> 
(a1*b1)+(a2*b2)/a1
(a3*b3)+(a4*b4)/a3
.
.
.

我不知道從哪里開始。 因此,感謝您的幫助。

您可以執行以下操作:

ind_denominator <- seq(1, nrow(dat), by=2)
ind_sum <- rep(ind_denominator, each=2)
tapply(dat$a*dat$b, ind_sum, sum)/dat$a[ind_dividor]

這給你:

          1           3           5           7           9 
-0.30248500 -0.29310345  2.50702143 -0.06849079 -0.24027604 
         11          13          15          17          19 
-0.15544296 -0.17268351  0.40555127 -0.34690180 -0.39212469

rowsum()另一種選擇:

with(df, rowsum(a * b / rep(a[c(T, F)], each = 2), (seq_along(a) - 1) %/% 2))

#         [,1]
#0 -0.30248500
#1 -0.29310345
#2  2.50702143
#3 -0.06849079
#4 -0.24027604
#5 -0.15544296
#6 -0.17268351
#7  0.40555127
#8 -0.34690180
#9 -0.39212469

這是僅使用seq的另一種方法:

(df[seq(1,nrow(df),2),1]*df[seq(1,nrow(df),2),2] + df[seq(2,nrow(df),2),1]*df[seq(2,nrow(df),2),2])/df[seq(1,nrow(df),2),1]

子集信息:

http://www.statmethods.net/management/subset.html
https://stat.ethz.ch/R-manual/R-devel/library/base/html/subset.html
https://stat.ethz.ch/R-manual/R-devel/library/base/html/nrow.html

如果您不想在R(*)中執行for循環,那么這是關於子集數據,“:”運算符和seq()或序列運算符的問題。 *循環並不像人們認為的那么糟糕,特別是通過子集計算功能[1]的合理使用(如pratz中的tapply()或集合函數的類似的行)(如inf中的rowsum())。但是,假設您當時不想循環您可以修改如下代碼:

mydata <- data.frame(a,b) # #Your data either matrix or data frame format. 
# In this case I used vectors or column a and b 
indexa <-  seq(1,nrow(mydata)-1, by = 2) #we to index a from 1 to 1 minus the last row
indexb <- seq(2,nrow(mydata), by = 2) #we want to index b from 2 to the last row
ans <- (mydata$a[1:indexa]*mydata$b[1:indexa] +
         mydata$a[2:indexb]*mydata$b[2:indexb])/(mydata$a[1:indexa])
ans = 
 [1] -0.30248500 -0.29310345  2.50702143 -0.06849079 -0.24027604 -0.15544296 -0.17268351
 [8]  0.40555127 -0.34690180 -0.39212469

希望您可能已經注意到R的一些有趣之處。它的外觀與您在SQL,Oracle,Python或Matlab中編寫的外觀非常相似! 實際上,就語法而言,R的基本包在數學上對SQL,Octave,數字Python和Matlab都是規范的,如果您知道這些語言中的一種,那么就可以理解它們。 由於我懷疑您可能知道另一種編碼語言,因此我在R和其他語言之間提供了非常著名的語法同義詞庫。

http://mathesaurus.sourceforge.net/r-numpy.html

http://mathesaurus.sourceforge.net/octave-r.html

可悲的是,我不認為Java和R是規范的,因為我還沒有看到兩種語言之間的語法詞庫。

另外,這里還有一些有關其他兩個解決方案提供商發布的功能的鏈接。 tapply()和rowsum()

https://www.r-bloggers.com/r-function-of-the-day-tapply-2/

https://stat.ethz.ch/R-manual/R-devel/library/base/html/rowsum.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM