Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. df1
  2. v1 v2 v3
  3. 1 1 2 a
  4. 2 2 3 b
  5. 3 3 4 c
  6. 4 4 5 d
  7. 5 3 5 e
  8.  
  9. df2
  10. v1 v2 v3
  11. 1 1 2 j
  12. 2 2 3 i
  13. 3 3 4 t
  14. 4 3 5 r
  15.  
  16. df3
  17. v1 v2 v3
  18. 1 2 3 t
  19. 2 2 4 g
  20. 3 6 7 i
  21. 4 8 9 t
  22.  
  23. v1 v2 v3
  24. 1 1 2 aj0
  25. 2 2 3 bit
  26. 3 2 4 00g
  27. 4 3 4 ct0
  28. 5 3 5 er0
  29. 6 4 5 d00
  30. 7 6 7 00i
  31. 8 8 9 00t
  32.  
  33. > df12 <- merge(df1, df2, by=c("v1", "v2"), all=TRUE)
  34. > df123 <- merge(df12, df3, by=c("v1", "v2"), all=TRUE)
  35. > df123$v3 <- paste0(df123[,3], df123[,4], df123[,5])
  36. > df123$v3.x <- df123$v3.y <- NULL
  37. > df123$v3 <- gsub("NA", "0", df123$v3)
  38.  
  39. dfs <- list(df1, df2, df3)
  40. merged <- Reduce(function(x, y) merge(x, y, by=c("v1", "v2"), all=T), dfs)
  41. v3 <- gsub("NA", "0", apply(merged[,c(-1, -2)], 1, paste, collapse=""))
  42. data.frame(v1=merged$v1, v2=merged$v2, v3)
  43. # v1 v2 v3
  44. # 1 1 2 aj0
  45. # 2 2 3 bit
  46. # 3 2 4 00g
  47. # 4 3 4 ct0
  48. # 5 3 5 er0
  49. # 6 4 5 d00
  50. # 7 6 7 00i
  51. # 8 8 9 00t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement