【问题标题】:How to match/merge data from two different files in R?如何匹配/合并来自 R 中两个不同文件的数据?
【发布时间】:2015-03-23 20:41:19
【问题描述】:

我有两个文件(file1.csv 和 file2.csv)。如下所示,file1 包含两列日期和具有 365 个观测值(全年)的变量 x1。文件 2 包含作为 file1 的列日期和许多其他变量。我只对只有 24 个观察值的变量 x45 感兴趣(每月 2 个观察值)。

文件1

date     x1
1/01/2005   33
2/01/2005   24
3/01/2005    72
31/12/2005   52

文件 2

date     x2      x3     x45
1/01/2005               115
5/02/2005                125
13/04/2005               127
31/12/2005               138

所以我想将 x45 列添加到 file1.csv 看起来像

date    x1    x45
1/01/2005   33  115
2/01/2005   24    NA
3/01/2005    72   NA
31/12/2005   52           138

我尝试过使用

file1= read.csv("D:/file1.csv")
file2= read.csv("D:/file2.csv")
file3 = merge(file1, file2)

但是,文件 3 只有 24 行(观察),并省略了文件 1 中的其余观察。

我们将不胜感激获得上述结果的任何帮助。

【问题讨论】:

  • @RichardScriven 不,他们不是。我只是忽略了写它们的值,因为我不需要它们。

标签: r merge match


【解决方案1】:

你可以试试left_join

library(dplyr)
left_join(df1, df2[c('date', 'x45')], by='date')
#         date x1 x45
#1  1/01/2005 33 115
#2  2/01/2005 24  NA
#3  3/01/2005 72  NA
#4 31/12/2005 52 138

或使用merge

merge(df1, df2[c('date', 'x45')], all.x=TRUE)
#       date x1 x45
#1  1/01/2005 33 115
#2  2/01/2005 24  NA
#3  3/01/2005 72  NA
#4 31/12/2005 52 138

更新

dplyr 中的left_joinplyr 中的join 保持原来的顺序。如果您需要在merge 中保留订单,一种选择是在“df1”中创建一个“indx”,在merge 之后,可以使用“indx”保留原始订单

df1$indx <- 1:nrow(df1)
 merge(df1, df2[c('date', 'x45')], all.x=TRUE)[order(df1$indx),-3]
    date x1 x45
 #1  1/01/2005 33 115
 #2  2/01/2005 24  NA
 #3  3/01/2005 72  NA
 #4 31/12/2005 52 138

或者使用来自plyrjoin

library(plyr)
join(df1, df2[c('date', 'x45')], by='date', type='left')

数据

df1 <- structure(list(date = c("1/01/2005", "2/01/2005", "3/01/2005", 
"31/12/2005"), x1 = c(33L, 24L, 72L, 52L)), .Names = c("date", 
"x1"), class = "data.frame", row.names = c(NA, -4L))

df2 <- structure(list(date = c("1/01/2005", "5/02/2005", "13/04/2005", 
"31/12/2005"), x2 = c(NA, NA, NA, NA), x3 = c(NA, NA, NA, NA), 
x45 = c(115L, 125L, 127L, 138L)), .Names = c("date", "x2", 
 "x3", "x45"), class = "data.frame", row.names = c(NA, -4L))

【讨论】:

  • left_join 不适用于我。但是,合并确实有效。非常感谢您的时间和帮助。
  • @aelwan 不知道为什么它不起作用。错误信息是什么?
  • 没有错误信息但是x45的所有值都是NA。
  • 如果我在不同的文件中有不同的 x45 列怎么办?
  • @Akurn 您的代码更改了行的顺序。有什么办法可以避免这种情况吗?
【解决方案2】:

为了完整起见,您可以使用data.table 包以非常快速的方式和通过引用(不使用&lt;-)加入和更新file1

library(data.table)
setkey(setDT(file1), date)[file2, x45 := i.x45]
file1
#          date x1 x45
# 1:  1/01/2005 33 115
# 2:  2/01/2005 24  NA
# 3:  3/01/2005 72  NA
# 4: 31/12/2005 52 138

在这里,您可以通过 date 列键入 file1,并在仅拉取 x45 列的同时对 file2 执行二元连接

【讨论】:

  • 感谢您的宝贵时间和帮助
【解决方案3】:

以下内容也可以使用,无需包,也无需更改 df1 中行的原始顺序:

df1
#        date x1
#2  1/01/2005 33
#3  2/01/2005 24
#4  3/01/2005 72
#5 31/12/2005 52
df2
#        date x45
#1  1/01/2005  33
#2  2/01/2005  24
#3  3/01/2005  72
#4 31/12/2005  52

df1$x45 <- df2$x45[match(df1$date, df2$date)]

df1
#        date x1 x45
#2  1/01/2005 33  33
#3  2/01/2005 24  24
#4  3/01/2005 72  72
#5 31/12/2005 52  52

【讨论】:

  • 感谢您的帮助。但是,我在所有 x45 列中都得到了 NA
猜你喜欢
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 2016-12-21
  • 2018-03-25
相关资源
最近更新 更多