Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > part1<-data.frame(key=c(5,6,7,8,9),x=c("b","d","a","c","b"))
- > part1
- key x
- 1 5 b # key==5,x==b
- 2 6 d
- 3 7 a
- 4 8 c
- 5 9 b
- > part2<-data.frame(key=c(1,2,3,4,5), x=c("c","a","b","d","a"))
- > part2
- key x
- 1 1 c
- 2 2 a
- 3 3 b
- 4 4 d
- 5 5 a # key==5,x==a
- dflist1<-list(part1,part2)
- final<-do.call(rbind,dflist1)
- final<-final[order(final$key),] #sort by key
- > final
- key x
- 6 1 c
- 7 2 a
- 8 3 b
- 9 4 d
- 1 5 b #duplicate from part1
- 10 5 a #duplicate from part2
- 2 6 d
- 3 7 a
- 4 8 c
- 5 9 b
- key x
- 6 1 c
- 7 2 a
- 8 3 b
- 9 4 d
- 10 5 a #this is from part2, no more duplicate from part1
- 2 6 d
- 3 7 a
- 4 8 c
- 5 9 b
- ## Create many data.frames
- set.seed(1)
- ## Normally, do this in lapply...
- part1 <- data.frame(key=1:6, x=sample(letters, 6))
- part2 <- data.frame(key=4:8, x=sample(letters, 5))
- part3 <- data.frame(key=8:12, x=sample(letters, 5))
- library(data.table)
- ## Collect all your "parts"
- PARTS.LIST <- lapply(ls(pattern="^part\d+"), function(x) get(x))
- DT.part <- rbindlist(PARTS.LIST)
- setkey(DT.part, key)
- unique(DT.part, by="key")
- ORIGINAL UNIQUE
- --------- -----------
- > DT.part > unique(DT.part, by="key")
- key x key x
- 1: 1 l 1: 1 l
- 2: 2 v 2: 2 v
- 3: 3 k 3: 3 k
- 4: 4 q 4: 4 q
- 5: 4 i 5: 5 r
- 6: 5 r 6: 6 f
- 7: 5 w 7: 7 v
- 8: 6 f 8: 8 f
- 9: 6 o 9: 9 j
- 10: 7 v 10: 10 d
- 11: 8 f 11: 11 g
- 12: 8 l 12: 12 m
- 13: 9 j
- 14: 10 d
- 15: 11 g
- 16: 12 m
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement