我正在尝试合并大约30个数据帧。
我已将全局环境保存为矢量,逗号分隔,如下所示;
df_names <- (df1, df2, df3, df4)
现在我正在尝试合并所有这些数据帧
total <- merge(df_names, by = 'ID')
但我收到了错误;
Error in as.data.frame(y) : argument "y" is missing, with no default
答案 0 :(得分:3)
将评论转换为答案,您可能正在寻找mget
和Reduce
以及merge
的组合。
演示:
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