Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- library(magrittr)
- DT <- fread('
- id click
- 1 a
- 1 a
- 1 b
- 1 a
- 1 c
- 1 c
- 1 a
- 1 a
- 1 b
- 1 b
- 1 b
- 1 a
- 2 a
- 2 a
- 2 b
- 2 c
- 2 a
- 2 b
- 2 c')
- indx <- DT %$% paste0(id, click) %>% rle %$%
- cumsum(c(1, lengths)) %>% .[1:(length(.)-1)]
- DT[indx, ]
- # id click
- # 1: 1 a
- # 2: 1 b
- # 3: 1 a
- # 4: 1 c
- # 5: 1 a
- # 6: 1 b
- # 7: 1 a
- # 8: 2 a
- # 9: 2 b
- # 10: 2 c
- # 11: 2 a
- # 12: 2 b
- # 13: 2 c
Advertisement
Add Comment
Please, Sign In to add comment