R code to assign a sequence based off of multiple variables [duplicate]

对着背影说爱祢 提交于 2020-01-30 08:09:32

问题


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:


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")


来源:https://stackoverflow.com/questions/59440563/r-code-to-assign-a-sequence-based-off-of-multiple-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!