2

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|

Bret
  • 23
  • 3
  • I appreciate that you've apologised for the bad format, but rather, please just put it in a good format. You can review the guide on how to make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). You can use `dput` to copy the structure of your data – Conor Neilson Mar 25 '20 at 05:01

1 Answers1

0

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)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Hi, I've converted this to a data frame and its just giving back a list of the paychecks. IE, its just the above dataset without the date which isn't really useful... How do I aggregate this so that the total yearly salary on the second paycheck is appropriately displayed as 2,400? – Bret Mar 24 '20 at 22:24
  • Hi, the first method adds another column (which is what I want) but basically just adds another paycheck column. The second one adds another column which is the total yearly salary which also doesn't work because I don't want the total salary but what they've been paid to date. – Bret Mar 24 '20 at 22:29
  • I have updated my question to show the table I want at the end. Hopefully this helps – Bret Mar 24 '20 at 22:34
  • 1
    Thank you very much this has answered the question and I have marked it. – Bret Mar 24 '20 at 22:36