0

I hope anyone can help with this. I have a data frame similar to this:

test <- data.frame(ID = c(1:24),
                  group = rep(c(1,1,1,1,1,1,2,2,2,2,2,2),2),
                  year1 = rep(c(2018,2018,2018,2019,2019,2019),4),
                  month1 = rep(c(1,2,3),8))

Now I want to do a cumsum per group but when I use the following code the sumsum restarts each year

test2 <-test %>% 
  group_by(group,year1,month1) %>% 
  summarise(a = length(unique(ID)))  %>%
  mutate(a = cumsum(a))

My desired output is:

   group year1 month1  a
1      1  2018      1  2
2      1  2018      2  4
3      1  2018      3  6
4      1  2019      1  8
5      1  2019      2 10
6      1  2019      3 12
7      2  2018      1  2
8      2  2018      2  4
9      2  2018      3  6
10     2  2019      1  8
11     2  2019      2 10
12     2  2019      3 12
Joep_S
  • 481
  • 4
  • 22
  • 1
    you want to add a row number to each group. Do `test %>% group_by(group) %>% mutate(a = row_number())` – Ronak Shah Dec 11 '19 at 08:44
  • when you use `group_by` on `group` and then ask for `length(unique(group))` the result is always one. I am not sure what you are trying to achieve. – Cettt Dec 11 '19 at 08:46
  • I editet my post since my data is more complicated. In my previous example the group/year/month combinations were present only once but in my actual data this can occur more often. Additionallt, I forgot to mention the variable ID – Joep_S Dec 11 '19 at 09:38

0 Answers0