0

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 Answer 1

3

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)))
Sign up to request clarification or add additional context in comments.

9 Comments

If I use this function it gives this error :Error in Reduce(function(...) merge(..., mget(v1[i:length(v1)]))) : argument "x" is missing, with no default
@sidsatam I think there was a mismatching ). Please check it now
it now giving me following error: Error: value for ‘df1’ not found
@sidsatam You didn't providie even a reproducible example. So, please update your post with an example for others to work on
@sidsatam Based on the error, it means you don't have an object 'df1'. So, the error is in your part and not in the code. Your description says that you have df1,df2,df3,df4,df5 and df6. .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.