roel
roel

Reputation: 43

Subsequent row summing in dataframe object

I would like to do subsequent row summing of a columnvalue and put the result into a new columnvariable without deleting any row by another columnvalue .

Below is some R-code and an example that does the trick and hopefully illustrates my question. I was wondering if there is a more elegant way to do since the for loop will be time consuming in my actual object.

Thanks for any feedback.

As an example dataframe:

MyDf <- data.frame(ID = c(1,1,1,2,2,2), Y = 1:6)
MyDf$FIRST <- c(1,0,0,1,0,0)
MyDf.2 <- MyDf
MyDf.2$Y2 <- c(1,3,6,4,9,15)

The purpose of this is so that I can write code that calculates Y2 in MyDf.2 above for each ID, separately.

This is what I came up with and, it does the trick. (Calculating a TEST column in MyDf that has to be equal to Y2 cin MyDf.2)

MyDf$TEST <- NA
for(i in 1:length(MyDf$Y)){
MyDf[i,]$TEST <- ifelse(MyDf[i,]$FIRST == 1, MyDf[i,]$Y,MyDf[i,]$Y + MyDf[i-1,]$TEST)
}

MyDf
  ID Y FIRST TEST
1  1 1     1    1
2  1 2     0    3
3  1 3     0    6
4  2 4     1    4
5  2 5     0    9
6  2 6     0   15


MyDf.2
   ID Y FIRST Y2
1  1 1     1  1
2  1 2     0  3
3  1 3     0  6
4  2 4     1  4
5  2 5     0  9
6  2 6     0 15

Upvotes: 3

Views: 122

Answers (1)

Jilber Urbina
Jilber Urbina

Reputation: 61154

You need ave and cumsum to get the column you want. transform is just to modify your existing data.frame.

> MyDf <- transform(MyDf, TEST=ave(Y, ID, FUN=cumsum))
  ID Y FIRST TEST
1  1 1     1    1
2  1 2     0    3
3  1 3     0    6
4  2 4     1    4
5  2 5     0    9
6  2 6     0   15

Upvotes: 2

Related Questions