I have data on yields of multipick crops across several weeks of picking. I would like to add the yields for each week to show an accumulation across the season.
Here is a small reproducible example of my data.
> mydata <- structure(list(cultivar = c(1, 1, 1, 2, 2, 2), harvestweek = c(0, 1, 2, 0, 1, 2), measure = c(10, 12, 8, 7, 0, 0)), .Names = c("cultivar", "harvestweek", "measure"), row.names = c(NA, -6L), class = "data.frame")
> mydata
cultivar harvestweek measure
1 1 10
1 2 12
1 3 8
2 1 7
2 2 0
2 3 8
I would like to generate a new variable that is the "accumulated yields". That is, I would like
cultivar harvestweek measure accum
1 1 10 10
1 2 12 22
1 3 8 30
2 1 7 7
2 2 0 7
2 3 8 15
Is there an easy way to do this programmatically? Perhaps with dplyr?
I've tried cannibalizing and futzing with dplyr to calculate "changes from the baseline", using harvestweek 1 as the baseline, but I can't figure out how to get the additional observations to accumulate onto each other, sequentially, starting with week 1.