I have a dataset with multiple observations nested within individuals. This example dataset includes columns for id and for day of the week (dayweek, 1-7). I have observations from 3 days from each individual. So one individual might have only submitted reports for Sun/Wed/Thu (1, 4, 5), and the other might have submitted reports for Sun/Mon/Tue (1, 2, 3), as in this example:
df <- data.frame(
id = c(rep(1:2, each = 6),2),
dayweek = c(rep(c(1, 4, 5), each = 2),rep(c(1, 2, 3), each = 2), 3)
)
I want to set up a column that marks each individual's first, second, and third day, like this:
df2 <- data.frame(
id = c(rep(1:2, each = 6),2),
dayweek = c(rep(c(1, 4, 5), each = 2),rep(c(1, 2, 3), each = 2), 3),
daynum = c(rep(1:3, each = 2, times = 2), 3)
)
I tried using
df %>% group_indices(id, dayweek)
but this produces a new id for each individual-day combination. What's a good way to do this?
Thanks in advance!