如何将data.frame列表追加到data.frame进行合并? [英] How to append a list of data.frames to a data.frame for merge?

查看:118
本文介绍了如何将data.frame列表追加到data.frame进行合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆