I have a dataset in R as below:
id <- c(1,1,1,1,1,2,2,2,2,3,3)
time <- c(2000,2001,2002,2003,2004,2000,2001,2002,2003,2000,2001)
group <- c(0,0,1,0,0,0,1,0,1,0,0)
df_temp <- data.frame(id, time, group)
and would like to create a new variable called "n" to record the sequence by "group" and re-start every time "group" switch from 0 to 1 or 1 to 0 as below:
n <- c(1,2,1,1,2,1,1,1,1,1,2)
Please could you suggest how I could generate variable "n" using dplyr package in R? Thanks very much, in advance.
I tried:
df_temp2 <-
df_temp %>%
arrange(id, time, group) %>%
group_by(group) %>%
mutate(n=seq_along(group))
but "n" does not return as what I expected.