1

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?

cait483
  • 13
  • 3
  • 1
    Isn't the expected column same as `Day` Try `df1 %>% group_by(ID) %>% mutate(desired = match(Day, unique(Day)))` – akrun Dec 21 '19 at 23:22
  • @akrun Sorry I definitely messed the day numbers up... I edited it to the correct numbers – cait483 Dec 21 '19 at 23:40

1 Answers1

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")
akrun
  • 874,273
  • 37
  • 540
  • 662