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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…