R code to assign a sequence based off of multiple

2020-04-19 08:10发布

I have data structured as below:

ID   Day   Desired Output
1    1      1
1    1      1
1    1      1
1    2      2
1    2      2
1    3      3
2    4      1
2    4      1
2    5      2
3    6      1
3    6      1

Is it possible to create a sequence for the desired output without using a loop? The dataset is quite large so a loop won't work, is it possible to do this with the dplyr package or maybe a combination of cumsum/diff?

1条回答
smile是对你的礼貌
2楼-- · 2020-04-19 08:56

An option is to group by 'ID', and then do a match on the 'Day' with the unique values of 'Day' column

library(dplyr)
df1 %>% 
    group_by(ID) %>% 
    mutate(desired = match(Day, unique(Day))) 

data

df1 <- structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L), Day = c(1L, 1L, 1L, 2L, 2L, 3L, 4L, 4L, 5L, 6L, 6L)), row.names = c(NA, 
-11L), class = "data.frame")
查看更多
登录 后发表回答