简体   繁体   中英

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 . I would like to merge these files to an existing data.frame which is currently not in the list (call it 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) . 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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM