pratz
pratz

Reputation: 19

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

i have a dataframe as follows

      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

so what i want in this case is a list in the following way:

(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
.
.
.

i have no idea where to start. so any help is appreciated.

Upvotes: 1

Views: 476

Answers (4)

mlane
mlane

Reputation: 131

Subsettting Info:

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

If you don't want to do a for loop in R(*), then this is about subsetting data, the ":" operator, and seq() or sequence operator. *Loops are not as bad as people think especially through elegent uses of the subset computation function[1], like tapply() in pratz, or elegent uses of the aggregation function like rowsum() in. However, supposed you wanted no loops then you might right a code like this:

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

Hopefully you might have noticed something interesting about R. Its very looks very similar to something you would write in SQL, Oracle, Python, or Matlab! In fact syntax-wise the base package of R is mathematically canonical to SQL, Octave, numeric Python, and Matlab, which is math-proof-speak for if you know one of these languages you know them all. Since I suspect you might know another coding language I have provided a very famous syntax thesaurus between R and these other languages.

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

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

sadly I don't think java and R are canonical as I have yet to see a syntax-thesaurus between the two languages.

Also here are some links about the functions the other two solutions providers posted. tapply() and 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

Upvotes: 0

Carles Mitjans
Carles Mitjans

Reputation: 4866

Here is a different approach in one line using only 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]

Upvotes: 0

akuiper
akuiper

Reputation: 214927

Another option with 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

Upvotes: 1

Rentrop
Rentrop

Reputation: 21497

You can do the following:

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]

Which gives you:

          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

Upvotes: 1

Related Questions