[英]How to append a list of data.frames to a data.frame for merge?
I have a list of data.frames from an lapply
, let's call it dflist
. 我有一个lapply的
lapply
列表,我们称它为dflist
。 I would like to merge these files to an existing data.frame which is currently not in the list (call it master
). 我想将这些文件合并到当前不在列表中的data.frame(称为
master
)中。
If I had a new list of data.frames where master
is the first element, and the next elements are the elements from dflist
I could issue Reduce(merge, new_list)
. 如果我有一个新的data.frames列表,其中
master
是第一个元素,接下来的元素是dflist
中的元素, dflist
可以发出Reduce(merge, new_list)
。 How can I arrive to this list? 我如何到达此清单?
Here is a reproducible example: 这是一个可重现的示例:
library(data.table)
master <- data.table(id = 1:5, var = c("A", "B", "C", "D", "E"))
setkey(master, "id")
dflist <- list(data.table(id = 1:5, z1 = runif(5)), data.table(id = 1:5, z2 = runif(5)))
lapply(dflist, function(x) {setkey(x, "id")})
Reduce(merge, dflist)
works perfectly. Reduce(merge, dflist)
完美工作。 I would like to include master
into the list as the first element to merge each z variable in the data.tables to that file. 我想将
master
包含在列表中,作为将data.tables中的每个z变量合并到该文件的第一个元素。
也许
Reduce(merge, dlist, init=master)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.