celestialgod

delete near duplicated row

Mar 17th, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.50 KB | None | 0 0
  1. library(data.table)
  2. library(magrittr)
  3. DT <- fread('
  4. id click
  5. 1  a
  6. 1  a
  7. 1  b
  8. 1  a
  9. 1  c
  10. 1  c
  11. 1  a
  12. 1  a
  13. 1  b
  14. 1  b
  15. 1  b
  16. 1  a
  17. 2  a
  18. 2  a
  19. 2  b
  20. 2  c
  21. 2  a
  22. 2  b
  23. 2  c')
  24.  
  25. indx <- DT %$% paste0(id, click) %>% rle %$%
  26.   cumsum(c(1, lengths)) %>% .[1:(length(.)-1)]
  27. DT[indx, ]
  28. #     id click
  29. #  1:  1     a
  30. #  2:  1     b
  31. #  3:  1     a
  32. #  4:  1     c
  33. #  5:  1     a
  34. #  6:  1     b
  35. #  7:  1     a
  36. #  8:  2     a
  37. #  9:  2     b
  38. # 10:  2     c
  39. # 11:  2     a
  40. # 12:  2     b
  41. # 13:  2     c
Advertisement
Add Comment
Please, Sign In to add comment