I'm trying to calculate the cumulative sum of a column in my data frame, but I only want it to be done based on a condition.
I am very new to R and trying to convert this spss code below in R to achieve the dataframe below with a new column called cumulative based on whether the lca is the same as the row above and if it is then add health net cost to the cumulative cost above.
spss code: IF LAG(LCA) NE LCA cumulative=health_net_cost. IF LAG(LCA)=LCA cumulative=LAG(cumulative)+health_net_cost. EXECUTE.
Dataframe in R:
Lca health_net_cost cummulative
10 100 100
10 200 300
10 300 600
28 400 1000
28 100 1100
8 100 1200
8 200 1400
8 300 1700
Aucun commentaire:
Enregistrer un commentaire