如何将data.frame列表追加到data.frame进行合并? [英] How to append a list of data.frames to a data.frame for merge?
问题描述
我有一个来自lapply
的data.frames列表,我们称它为dflist
.我想将这些文件合并到当前不在列表中的现有data.frame(将其称为master
).
I have a list of data.frames from an lapply
, let's call it dflist
. I would like to merge these files to an existing data.frame which is currently not in the list (call it master
).
如果我有一个新的data.frames列表,其中master
是第一个元素,接下来的元素是dflist
中的元素,则可以发布Reduce(merge, new_list)
.我如何到达此清单?
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)
. How can I arrive to this list?
以下是可重现的示例:
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)
完美运行.我想将master
包括在列表中,作为将data.tables中的每个z变量合并到该文件的第一个元素.
Reduce(merge, dflist)
works perfectly. 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.
推荐答案
也许
Reduce(merge, dlist, init=master)
这篇关于如何将data.frame列表追加到data.frame进行合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!