R code to assign a sequence based off of multiple variables [duplicate]
问题 This question already has answers here : Recode dates to study day within subject (2 answers) Closed last month . 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