Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. > part1<-data.frame(key=c(5,6,7,8,9),x=c("b","d","a","c","b"))
  2. > part1
  3. key x
  4. 1 5 b # key==5,x==b
  5. 2 6 d
  6. 3 7 a
  7. 4 8 c
  8. 5 9 b
  9. > part2<-data.frame(key=c(1,2,3,4,5), x=c("c","a","b","d","a"))
  10. > part2
  11. key x
  12. 1 1 c
  13. 2 2 a
  14. 3 3 b
  15. 4 4 d
  16. 5 5 a # key==5,x==a
  17.  
  18. dflist1<-list(part1,part2)
  19. final<-do.call(rbind,dflist1)
  20. final<-final[order(final$key),] #sort by key
  21.  
  22. > final
  23. key x
  24. 6 1 c
  25. 7 2 a
  26. 8 3 b
  27. 9 4 d
  28. 1 5 b #duplicate from part1
  29. 10 5 a #duplicate from part2
  30. 2 6 d
  31. 3 7 a
  32. 4 8 c
  33. 5 9 b
  34.  
  35. key x
  36. 6 1 c
  37. 7 2 a
  38. 8 3 b
  39. 9 4 d
  40. 10 5 a #this is from part2, no more duplicate from part1
  41. 2 6 d
  42. 3 7 a
  43. 4 8 c
  44. 5 9 b
  45.  
  46. ## Create many data.frames
  47.  
  48. set.seed(1)
  49.  
  50. ## Normally, do this in lapply...
  51. part1 <- data.frame(key=1:6, x=sample(letters, 6))
  52. part2 <- data.frame(key=4:8, x=sample(letters, 5))
  53. part3 <- data.frame(key=8:12, x=sample(letters, 5))
  54.  
  55. library(data.table)
  56.  
  57. ## Collect all your "parts"
  58. PARTS.LIST <- lapply(ls(pattern="^part\d+"), function(x) get(x))
  59.  
  60.  
  61. DT.part <- rbindlist(PARTS.LIST)
  62.  
  63. setkey(DT.part, key)
  64. unique(DT.part, by="key")
  65.  
  66. ORIGINAL UNIQUE
  67. --------- -----------
  68. > DT.part > unique(DT.part, by="key")
  69. key x key x
  70. 1: 1 l 1: 1 l
  71. 2: 2 v 2: 2 v
  72. 3: 3 k 3: 3 k
  73. 4: 4 q 4: 4 q
  74. 5: 4 i 5: 5 r
  75. 6: 5 r 6: 6 f
  76. 7: 5 w 7: 7 v
  77. 8: 6 f 8: 8 f
  78. 9: 6 o 9: 9 j
  79. 10: 7 v 10: 10 d
  80. 11: 8 f 11: 11 g
  81. 12: 8 l 12: 12 m
  82. 13: 9 j
  83. 14: 10 d
  84. 15: 11 g
  85. 16: 12 m
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement