在 R 中合并多个具有不同行长的 data.frames [英] Merge multiple data.frames in R with varying row length

查看:24
本文介绍了在 R 中合并多个具有不同行长的 data.frames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 R 比较陌生,并试图弄清楚如何合并具有不同行数但都具有公共列Year"的多个 data.frames.我看过类似的问题,还有这个问题:合并不同长度的数据帧提供了很好的答案.但是,当我将它应用于我自己的数据时,我无法让它与多个 data.frames 一起工作;我总是收到错误消息.

I'm relatively new to R and trying to figure out how to merge multiple data.frames with varying numbers of rows but all with a common column, "Year". I've looked through similar questions, and this question: Merge dataframes, different lengths provided a great answer. However, when I applied it to my own data, I couldn't get it to work with multiple data.frames; I always receive an error message.

示例数据:

> df1 <- data.frame(Year=2006:2011, Site1=c("2.3", "1"  , "3.1", "2.9", "1.4", "3"))  
> df2 <- data.frame(Year=2007:2011, Site2=c("2.7", "4.1", "1.1", "2.6", "3.1"))  
> df3 <- data.frame(Year=2008:2011, Site3=c("1.3", "2"  , "3.6", "1.7"))  

目标是生成单个 data.frame,其中第 1 列是年份,第 2 列是站点 1,第 3 列是站点 2,依此类推.我目前有大约 17 个 data.frames(最多将有 40 个),对应于 17 个具有可变时间线/行数的站点.

The goal is to produce a single data.frame where column 1 is the year, column 2 is site 1, column 3 is site 2, and so on. I have ~17 data.frames currently (there will be up to 40), corresponding to 17 sites with variable timelines/number of rows.

任何帮助将不胜感激.

我尝试过的代码:

> NewDF <- merge(df1, df2, by="Year", all.x=TRUE, all.y=TRUE)  

这对 2 个 data.frame 很有效,但是当我尝试添加另一个 data.frame 时,我收到错误消息:

This worked great for 2 data.frames, but when I tried to add in another data.frame, I received the error message:

> NewDF <- merge(list=c(df1, df2, df3), by="Year", all.x=TRUE, all.y=TRUE)  
 Error in as.data.frame(x) : argument "x" is missing, with no default

推荐答案

您想将结果与 df3 合并,即:

You want to merge the result with df3, i.e.:

merge(df3, merge(df1, df2, by="Year", all.x=TRUE, all.y=TRUE), by = "Year", all.x = TRUE, all.y = TRUE)
#  Year Site3 Site1 Site2
#1 2006  <NA>   2.3  <NA>
#2 2007  <NA>     1   2.7
#3 2008   1.3   3.1   4.1
#4 2009     2   2.9   1.1
#5 2010   3.6   1.4   2.6
#6 2011   1.7     3   3.1

或者,如果您的 data.frame 在列表中,请使用 Reduce 概括上述内容:

Or if you have your data.frame's in a list, use Reduce to generalize the above:

Reduce(function(x,y) merge(x, y, by = "Year", all.x = TRUE, all.y = TRUE),
       list(df1, df2, df3))
#  Year Site1 Site2 Site3
#1 2006   2.3  <NA>  <NA>
#2 2007     1   2.7  <NA>
#3 2008   3.1   4.1   1.3
#4 2009   2.9   1.1     2
#5 2010   1.4   2.6   3.6
#6 2011     3   3.1   1.7

这篇关于在 R 中合并多个具有不同行长的 data.frames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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