Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- thedata <- data.frame(group= c(0,0,0,0,0,1,1,1,1,1)
- ,experiment = c(0,0,1,1,1,0,0,1,1,1)
- ,time = c(1,2,3,4,5,1,2,3,4,5))
- $`1`
- group experiment time
- 0 0 1
- 0 0 2
- 0 1 3
- 1 0 1
- 1 0 2
- 1 1 3
- $`2`
- group experiment time
- 0 0 1
- 0 0 2
- 0 1 3
- 0 1 4
- 1 0 1
- 1 0 2
- 1 1 3
- 1 1 4
- $`3`
- group experiment time
- 0 0 1
- 0 0 2
- 0 1 3
- 0 1 4
- 0 1 5
- 1 0 1
- 1 0 2
- 1 1 3
- 1 1 4
- 1 1 5
- library(data.table)
- setDT(thedata) # convert to data.table
- thedata[, split := cumsum(experiment), by = group] # create a splitting variable
- thelist = split(thedata, thedata$split) # split the data frame into experiments
- newlist = list(rbind(thelist[[1]], thelist[[(2)]])) # start to append list elements
- for(i in 2:(length(thelist) - 1)){
- # append each new experiment to the old appended data frame in the newlist
- newlist[[i]] = rbind(newlist[[i-1]], thelist[[(i+1)]])
- }
- newlist
- [[1]]
- group experiment time split
- 1: 0 0 1 0
- 2: 0 0 2 0
- 3: 1 0 1 0
- 4: 1 0 2 0
- 5: 0 1 3 1
- 6: 1 1 3 1
- [[2]]
- group experiment time split
- 1: 0 0 1 0
- 2: 0 0 2 0
- 3: 1 0 1 0
- 4: 1 0 2 0
- 5: 0 1 3 1
- 6: 1 1 3 1
- 7: 0 1 4 2
- 8: 1 1 4 2
- [[3]]
- group experiment time split
- 1: 0 0 1 0
- 2: 0 0 2 0
- 3: 1 0 1 0
- 4: 1 0 2 0
- 5: 0 1 3 1
- 6: 1 1 3 1
- 7: 0 1 4 2
- 8: 1 1 4 2
- 9: 0 1 5 3
- 10: 1 1 5 3
Add Comment
Please, Sign In to add comment