Reputation: 7107
I am trying to merge approx 30 dataframes.
I have saved the global environment as a vector, comma separated, as below;
df_names <- (df1, df2, df3, df4)
Now I am trying to merge all of these dataframes
total <- merge(df_names, by = 'ID')
But I am getting an error;
Error in as.data.frame(y) : argument "y" is missing, with no default
Upvotes: 1
Views: 363
Reputation: 193517
Converting comments to an answer, you're probably looking for a combination of mget
and Reduce
along with merge
.
Demo:
df1 <- data.frame(ID = 1:3, var = c("a", "b", "c"))
df2 <- data.frame(ID = c(1, 3, 4), var = c("A", "B", "X"))
df3 <- data.frame(ID = c(2, 3, 4, 5), var = c("X", "Y", "Z", "A"))
df4 <- data.frame(ID = 1:5, var = letters[1:5])
Reduce(function(x, y) merge(x, y, by = "ID", all = TRUE), mget(paste0("df", 1:4)))
# ID var.x var.y var.x var.y
# 1 1 a A <NA> a
# 2 2 b <NA> X b
# 3 3 c B Y c
# 4 4 <NA> X Z d
# 5 5 <NA> <NA> A e
# Warning message:
# In merge.data.frame(x, y, by = "ID", all = TRUE) :
# column names ‘var.x’, ‘var.y’ are duplicated in the result
Upvotes: 3