【问题标题】:Merging multiple two-column text files into one data frame based on one column基于一列将多个两列文本文件合并到一个数据框中
【发布时间】:2016-03-20 11:40:38
【问题描述】:

这个问题是this one 的后续问题,得到了否定的答复,没有答案。所以,我正在尝试使用 R 来做到这一点。

我有很多(超过 30 个)这样的文件:

  • 文件1

    5 A1
    23 A3
    1 B2
    289 BX5
    90 D3
    
  • 文件2

    2 A1
    10 A2
    3 B1
    1 BX4
    90 D3
    24 E0
    
  • 文件3

    4 A0
    11 A2
    1 B1
    2 D3
    

我想将所有这些组合起来生成一个这样的数据框:

A0 0 0 4
A1 5 2 0
A2 0 10 11
A3 23 0 0
B1 0 3 1
B2 1 0 0
BX4 0 1 0
BX5 289 0 0
D3 90 90 2
E0 0 24 0

基于this,我尝试使用 read.table 读取两个文件,同时将第二列指定为行名,然后按行名合并数据框,如下所示:

> df1 <- read.table("File1", row.names = 2)
> df1
     V1
A1    5
A3   23
B2    1
BX5 289
D3   90
> df2 <- read.table("File2", row.names = 2)
> df2
     V1
A1    2
A2   10
B1    3
BX4   1
D3   90
E0   24
> m1 <- merge(df1, df2, by=0, all=TRUE)
> m1[is.na(m1)] <- 0
> m1
    Row.names V1.x V1.y
1          A1    5    2
2          A2    0   10
3          A3   23    0
4          B1    0    3
5          B2    1    0
6         BX4    0    1
7         BX5  289    0
8          D3   90   90
9          E0    0   24

到目前为止一切顺利,但是当我尝试将生成的数据框合并到第三个数据框时,它并没有像我希望的那样工作。正因为如此,我不确定如何继续将所有 30 多岁的文件合并到一个数据框中。以前我以为我会修改here 描述的multmerge 函数,但现在我卡住了。

那么,有人可以帮我解决这个问题吗?提前致谢。

编辑:如果有人能为这个问题建议一个更好的标题,我也会非常感激。

【问题讨论】:

  • 由于这个问题被标记为重复(“之前已经被问过并且已经有答案”),标记它的人可以向我指出它被问到的地方吗?我在 Stackoverflow 中找不到它;还是我搜索不够努力? This question 对我来说和我的不同,我在那里看不到我的问题的任何答案。

标签: r merge read.table


【解决方案1】:

我已尝试根据您的问题调整 multmerge 功能中的 Reduce-part。

#read in the data (can be replaced with filenames
#like f1 <- read.table(file, header=F)
#or even lapply(list.files(mypath), read.table, header=F) 
#to get all dataframes in a list
f1 <- read.table(text="5 A1
23 A3
1 B2
289 BX5
90 D3", header=F)

f2 <- read.table(text="2 A1
10 A2
3 B1
1 BX4
90 D3
24 E0", header=F)

f3 <- read.table(text="4 A0
11 A2
1 B1
2 D3", header=F)

#put files in list
myfiles <- list(f1,f2,f3)

#changing colnames because I like keeping my data in order/knowing where it came from.
myfiles <- lapply(1:length(myfiles),function(x){
  r <- myfiles[[x]]
  colnames(r) <- c(paste0("f",x),"ID")
  r
})

#using the Reduce-function
res <- Reduce(function(x,y) {merge(x,y,all=T, by="ID")}, myfiles)
res[is.na(res)]<-0
res

> res
    ID  f1 f2 f3
1   A1   5  2  0
2   A3  23  0  0
3   B2   1  0  0
4  BX5 289  0  0
5   D3  90 90  2
6   A2   0 10 11
7   B1   0  3  1
8  BX4   0  1  0
9   E0   0 24  0
10  A0   0  0  4

【讨论】:

  • 谢谢!它按我想要的方式工作。所以,我好像从一开始就错了,按行合并,而应该按列合并,对吧?
  • 根据您提供的输入和输出,您正在按列合并。
【解决方案2】:

下面是使用dplyr 的方法。首先,您需要在不指定行名的情况下加载数据。下面,我重用了你的 file1、file2、file3 结构,但你也可以像使用 df1、df2、df3 一样以正确的格式阅读它们。您需要一个名称列才能加入。然后你连续两次full_join。然后我对数据进行排序并将 NA 更改为 0。

file1 <-data.frame(Names=rownames(file1),V1=file1,row.names = NULL)
file2 <-data.frame(Names=rownames(file2),V1=file2,row.names = NULL)
file3 <-data.frame(Names=rownames(file3),V1=file3,row.names = NULL)

library(dplyr)
out <-file1  %>%
full_join(file2,by = "Names") %>%
full_join(file3,by = "Names") %>%
arrange(Names)
out[is.na(out)]<-0
#> out
#   Names V1.x V1.y V1
#1     A0    0    0  4
#2     A1    5    2  0
#3     A2    0   10 11
#4     A3   23    0  0
#5     B1    0    3  1
#6     B2    1    0  0
#7    BX4    0    1  0
#8    BX5  289    0  0
#9     D3   90   90  2
#10    E0    0   24  0

更新

为了处理任意数量的文件,我们必须引入一个循环。

myfiles <- list(file1,file2,file3)
out <-file1                       #first file
for (i in myfiles[-1]){           #all but first file
out <-full_join(out,i,by = "Names")
}
out <-arrange(out,Names)
out[is.na(out)]<-0
out
> out
   Names V1.x V1.y V1
1     A0    0    0  4
2     A1    5    2  0
3     A2    0   10 11
4     A3   23    0  0
5     B1    0    3  1
6     B2    1    0  0
7    BX4    0    1  0
8    BX5  289    0  0
9     D3   90   90  2
10    E0    0   24  0

【讨论】:

  • 感谢您的回答。这是什么%&gt;%?我用谷歌搜索找不到它。以及如何为超过 3 个文件执行此操作?我们不需要多次输入 full_join,对吧?
  • 您如何将其推广到任意数量的文件?
  • @sentausa %&gt;% 是来自 magrittr 包的正向管道运算符。 dplyr 导入这个包。
  • @Heroka 对于任意数量的文件,您需要一个循环。查看我的更新答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-03
  • 2013-03-28
  • 1970-01-01
  • 2018-01-30
  • 2021-01-11
相关资源
最近更新 更多