0

I have the following sample data frame:

dates <- c("2021-01-01", "2021-01-03", "2021-01-06", "2021-01-02", "2021-01-04", "2021-01-06")
group <- c("A", "A", "A", "B", "B", "B")
values <- c(1, 5, 4, 2, 7, 3)

df <- data.frame(dates = as.Date(dates), group = group, values)
df

Can someone please tell me how I can compute a variable as the cumulated sum of values for each group (A and B) separately (+ chronologically)?

values_cumulated should be 1, 6, 10, 2, 9, 12

I was trying it with group_by() and mutate(values_cum = cumsum(values) ) but couldnt get it to work.

D. Studer
  • 1,711
  • 1
  • 16
  • 35

0 Answers0