0
votes

I have six data frames ie df1,df2,df3,df4,df5 and df6. I want merge them using function in the following manner:

df1<-data.frame(c(1:1000))

df2<-data.frame(c(1:10000))

df3<-data.frame(c(1:50000))

df4<-data.frame(c(1:3000))

df5<-data.frame(c(1:70000))

df6<-data.frame(c(1:90000))

First the function will merge df1 with df2 and it will create a data frame with the merged values of df1 & df2, then it will do the same for df1 and df3 and df1 and df4, and so on. Once this process is over it repeats the same process for df2,df3,df4,df5,df6. Please help.

1

1 Answers

3
votes

We can use Reduce with merge. In the OP's post, by variable is not mentioned, so it is also showed in the code below

v1 <- paste0("df", 1:6)
res <- lapply(seq_along(v1), function(i) Reduce(function(...) 
           merge(...), mget(v1[i:length(v1)], envir = .GlobalEnv)))