Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- df1
- v1 v2 v3
- 1 1 2 a
- 2 2 3 b
- 3 3 4 c
- 4 4 5 d
- 5 3 5 e
- df2
- v1 v2 v3
- 1 1 2 j
- 2 2 3 i
- 3 3 4 t
- 4 3 5 r
- df3
- v1 v2 v3
- 1 2 3 t
- 2 2 4 g
- 3 6 7 i
- 4 8 9 t
- v1 v2 v3
- 1 1 2 aj0
- 2 2 3 bit
- 3 2 4 00g
- 4 3 4 ct0
- 5 3 5 er0
- 6 4 5 d00
- 7 6 7 00i
- 8 8 9 00t
- > df12 <- merge(df1, df2, by=c("v1", "v2"), all=TRUE)
- > df123 <- merge(df12, df3, by=c("v1", "v2"), all=TRUE)
- > df123$v3 <- paste0(df123[,3], df123[,4], df123[,5])
- > df123$v3.x <- df123$v3.y <- NULL
- > df123$v3 <- gsub("NA", "0", df123$v3)
- dfs <- list(df1, df2, df3)
- merged <- Reduce(function(x, y) merge(x, y, by=c("v1", "v2"), all=T), dfs)
- v3 <- gsub("NA", "0", apply(merged[,c(-1, -2)], 1, paste, collapse=""))
- data.frame(v1=merged$v1, v2=merged$v2, v3)
- # v1 v2 v3
- # 1 1 2 aj0
- # 2 2 3 bit
- # 3 2 4 00g
- # 4 3 4 ct0
- # 5 3 5 er0
- # 6 4 5 d00
- # 7 6 7 00i
- # 8 8 9 00t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement