r - Simultaneously merge multiple data.frames in a list -
i have list of many data.frames want merge. issue here each data.frame differs in terms of number of rows , columns, share key variables (which i've called "var1"
, "var2"
in code below). if data.frames identical in terms of columns, merely rbind
, plyr's rbind.fill job, that's not case these data.
because merge
command works on 2 data.frames, turned internet ideas. got 1 here, worked in r 2.7.2, had @ time:
merge.rec <- function(.list, ...){ if(length(.list)==1) return(.list[[1]]) recall(c(list(merge(.list[[1]], .list[[2]], ...)), .list[-(1:2)]), ...) }
and call function so:
df <- merge.rec(my.list, by.x = c("var1", "var2"), by.y = c("var1", "var2"), = t, suffixes=c("", ""))
but in r version after 2.7.2, including 2.11 , 2.12, code fails following error:
error in match.names(clabs, names(xi)) : names not match previous names
(incidently, see other references error elsewhere no resolution).
is there way solve this?
another question asked how perform multiple left joins using dplyr in r . question marked duplicate of 1 answer here:
library(dplyr) x <- data_frame(i = c("a","b","c"), j = 1:3) y <- data_frame(i = c("b","c","d"), k = 4:6) z <- data_frame(i = c("c","d","a"), l = 7:9) list(x,y,z) %>% reduce(function(dtf1,dtf2) left_join(dtf1,dtf2,by="i"), .) # j k l #1 1 na 9 #2 b 2 4 na #3 c 3 5 7
you can perform full_join() , inner_join()
list(x,y,z) %>% reduce(function(dtf1,dtf2) full_join(dtf1,dtf2,by="i"), .) #source: local data frame [4 x 4] # j k l #1 1 na 9 #2 b 2 4 na #3 c 3 5 7 #4 d na 6 8 list(x,y,z) %>% reduce(function(dtf1,dtf2) inner_join(dtf1,dtf2,by="i"), .) #source: local data frame [1 x 4] # j k l #1 c 3 5 7
for sake of completeness, here base r version of full join
reduce(function(dtf1, dtf2) merge(dtf1, dtf2, = "i", = true), list(x,y,z)) # j k l #1 1 na 9 #2 b 2 4 na #3 c 3 5 7 #4 d na 6 8
Comments
Post a Comment