Guest User

Untitled

a guest
Apr 26th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. thedata <- data.frame(group= c(0,0,0,0,0,1,1,1,1,1)
  2. ,experiment = c(0,0,1,1,1,0,0,1,1,1)
  3. ,time = c(1,2,3,4,5,1,2,3,4,5))
  4.  
  5. $`1`
  6. group experiment time
  7. 0 0 1
  8. 0 0 2
  9. 0 1 3
  10. 1 0 1
  11. 1 0 2
  12. 1 1 3
  13.  
  14. $`2`
  15. group experiment time
  16. 0 0 1
  17. 0 0 2
  18. 0 1 3
  19. 0 1 4
  20. 1 0 1
  21. 1 0 2
  22. 1 1 3
  23. 1 1 4
  24.  
  25. $`3`
  26. group experiment time
  27. 0 0 1
  28. 0 0 2
  29. 0 1 3
  30. 0 1 4
  31. 0 1 5
  32. 1 0 1
  33. 1 0 2
  34. 1 1 3
  35. 1 1 4
  36. 1 1 5
  37.  
  38. library(data.table)
  39. setDT(thedata) # convert to data.table
  40. thedata[, split := cumsum(experiment), by = group] # create a splitting variable
  41.  
  42. thelist = split(thedata, thedata$split) # split the data frame into experiments
  43. newlist = list(rbind(thelist[[1]], thelist[[(2)]])) # start to append list elements
  44.  
  45. for(i in 2:(length(thelist) - 1)){
  46. # append each new experiment to the old appended data frame in the newlist
  47. newlist[[i]] = rbind(newlist[[i-1]], thelist[[(i+1)]])
  48. }
  49.  
  50. newlist
  51. [[1]]
  52. group experiment time split
  53. 1: 0 0 1 0
  54. 2: 0 0 2 0
  55. 3: 1 0 1 0
  56. 4: 1 0 2 0
  57. 5: 0 1 3 1
  58. 6: 1 1 3 1
  59.  
  60. [[2]]
  61. group experiment time split
  62. 1: 0 0 1 0
  63. 2: 0 0 2 0
  64. 3: 1 0 1 0
  65. 4: 1 0 2 0
  66. 5: 0 1 3 1
  67. 6: 1 1 3 1
  68. 7: 0 1 4 2
  69. 8: 1 1 4 2
  70.  
  71. [[3]]
  72. group experiment time split
  73. 1: 0 0 1 0
  74. 2: 0 0 2 0
  75. 3: 1 0 1 0
  76. 4: 1 0 2 0
  77. 5: 0 1 3 1
  78. 6: 1 1 3 1
  79. 7: 0 1 4 2
  80. 8: 1 1 4 2
  81. 9: 0 1 5 3
  82. 10: 1 1 5 3
Add Comment
Please, Sign In to add comment