简体   繁体   English

如何将data.frame列表追加到data.frame进行合并?

[英]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.

相关问题 通过循环将多个data.frame合并为一个data.frame - Merge several data.frames into one data.frame with a loop 将重叠的data.frames列表转换为单个data.frame - Convert list of overlapping data.frames into single data.frame R 将列添加到 data.frame 中,即在 data.frames 列表中 - R Add column into data.frame, that is in list of data.frames R Shiny:如何使用上传的 data.frame 扩展包含 data.frames 列表的反应? - R Shiny: How to expand a reactive containing a list of data.frames with an uploaded data.frame? 如何获取data.frames列表中特定data.frame的索引? - How do I get the index of a specific data.frame in a list of data.frames? 对于具有多种列格式的data.frame,如何基于相同的列值跨data.frame行合并数据 - how to merge data across data.frame rows based on identical column values, for data.frames with multiple column formats 将 data.frames 列表(nx 2 data.frames)更整洁地重塑为单个 data.frame(nx 3 列) - Tidier reshape for list of data.frames (n x 2 data.frames) to single data.frame (n x 3 columns) 将数字类转换为data.frame后尝试合并data.frames的问题 - Issue trying to merge data.frames after converting numeric class to data.frame Data.frame合并和选择2个Data.frame中通用的值 - Data.frame Merge and Selection of values which are common in 2 Data.frames 按组将 data.frame 拆分为向量列表而不是 data.frames 列表 - Split a data.frame by group into a list of vectors rather than a list of data.frames
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM