How can I aggregate a "Change in" variable to a total variable?

Viewed 25

I have a data set in R that looks like the following:

|Date | Paycheck |


|Jan 1st | 1,000|


|Jan 14 | |1,400|


(Sorry for the bad format). I want to create another column that is total yearly salary (so far). I've tried an aggregate function but I get a small data set of ~8 observations when my total data set is ~80. I already have my data broken down by "employee" essentially, so all I need to do is aggregate in my data set.

Thanks for any help.

What I want is:

|Date | Paycheck |Yearly salary to date |


|Jan 1st | 1,000 |1,000|


|Jan 14 | |1,400 |2,400|

1 Answers

We can use ave in base R if we want to group by 'date' and create a new column

df1$new <- with(df1, ave(Paycheck, Date, FUN = cumsum))

If there is no groupinig

df1$new <- cumsum(df1$Paycheck)
Related