merge() multiple data frames (do.call ?)

Posted by Vincent on Stack Overflow See other posts from Stack Overflow or by Vincent
Published on 2010-06-12T12:53:17Z Indexed on 2010/06/12 13:02 UTC
Read the original article Hit count: 449

Filed under:

Hi everyone, here's my very simple question:

merge() only takes two data frames as input. I need to merge a series of data frames from a list, using the same keys for every merge operation. Given a list named "test", I want to do something like: do.call("merge", test). I could write some kind of loop, but I'm wondering if there's a standard or built-in way to do this more efficiently.

Any advice is appreciated. Thanks!


Here's a subset of the dataset in dput format (note that merging on country is trivial in this case, but that there are more countries in the original data):

test <-
list(structure(list(country = c("United States", "United States", 
"United States", "United States", "United States"), NY.GNS.ICTR.GN.ZS = c(13.5054687, 
14.7608697, 14.1115876, 13.3389063, 12.9048351), year = c(2007, 
2006, 2005, 2004, 2003)), .Names = c("country", "NY.GNS.ICTR.GN.ZS", 
"year"), row.names = c(NA, 5L), class = "data.frame"), structure(list(
    country = c("United States", "United States", "United States", 
    "United States", "United States"), NE.TRD.GNFS.ZS = c(29.3459277, 
    28.352838, 26.9861939, 25.6231246, 23.6615328), year = c(2007, 
    2006, 2005, 2004, 2003)), .Names = c("country", "NE.TRD.GNFS.ZS", 
"year"), row.names = c(NA, 5L), class = "data.frame"), structure(list(
    country = c("United States", "United States", "United States", 
    "United States", "United States"), NY.GDP.MKTP.CD = c(1.37416e+13, 
    1.31165e+13, 1.23641e+13, 1.16309e+13, 1.0908e+13), year = c(2007, 
    2006, 2005, 2004, 2003)), .Names = c("country", "NY.GDP.MKTP.CD", 
"year"), row.names = c(NA, 5L), class = "data.frame"))

© Stack Overflow or respective owner

Related posts about r