如何在R中合并多个不同维度的矩阵

时间:2016-04-25 05:23:02

标签: r

我有这些不同维度的矩阵。所有矩阵中的key.related.sheet列都有一些常见值和一些唯一值。我想匹配这些常见的行并合并所有三个矩阵,但我也想包含唯一的行。结果列应仅包含key.related.sheetSample_Btrace_1trace_2trace_3列。有人可以帮帮我吗?

aa<-structure(c("S05-F13-P01:S05-F13-P01", "S05-F13-P01:S08-F10-P01", 
"S05-F13-P01:S08-F11-P01", "S05-F13-P01:S09-F66-P01", "S05-F13-P01", 
"S08-F10-P01", "S08-F11-P01", "S09-F66-P01", "1.25", "0.227", 
"-0.183", "-0.217"), .Dim = c(4L, 3L), .Dimnames = list(NULL, 
    c("key.related.sheet", "sample_B", "trace_1")))

bb<-structure(c("S05-F13-P01:S08-F10-P01", "S05-F13-P01:S08-F11-P01", 
"S05-F13-P01:S09-F66-P01", "S05-F13-P01:S09-F67-P01", "S08-F10-P01", 
"S08-F11-P01", "S09-F66-P01", "S09-F67-P01", "0.227", "-0.183", 
"-0.217", "0.292", "Unknown", "Unknown", "Unknown", "Unknown"
), .Dim = c(4L, 4L), .Dimnames = list(NULL, c("key.related.sheet", 
"sample_B", "trace_2", "type")))

cc<-structure(c("S05-F13-P01:S08-F11-P01", "S05-F13-P01:S09-F66-P01", 
"S05-F13-P01:S09-F67-P01", "S05-F13-P01:S09-F68-P01", "S05-F13-P01:S09-F01-P01", 
"S08-F11-P01", "S09-F66-P01", "S09-F67-P01", "S09-F68-P01", "S09-F01-P01", 
"-0.183", "-0.217", "0.292", "-0.314", "0.0418"), .Dim = c(5L, 
3L), .Dimnames = list(NULL, c("key.related.sheet", "sample_B", 
"trace_3")))

预期输出为:

   key.related.sheet         sample_B      trace_1   trace_2    trace_3
 "S05-F13-P01:S05-F13-P01" "S05-F13-P01" "1.25"  
 "S05-F13-P01:S08-F10-P01" "S08-F10-P01" "0.227"      "0.227"
 "S05-F13-P01:S08-F11-P01" "S08-F11-P01" "-0.183"     "-0.183"    "-0.183"   
 "S05-F13-P01:S09-F66-P01" "S09-F66-P01" "-0.217"     "-0.217"    "-0.217"
 "S05-F13-P01:S09-F67-P01" "S09-F67-P01"              "0.292"     "0.292"
 "S05-F13-P01:S09-F68-P01" "S09-F68-P01"                          "-0.314"
 "S05-F13-P01:S09-F01-P01" "S09-F01-P01"                          "0.0418"

4 个答案:

答案 0 :(得分:5)

可以使用Reducemerge的组合完成此操作,如下所示:

Reduce(function(x, y) merge(x, y, all=TRUE), list(aa, bb[,-4], cc))

结果:

        key.related.sheet    sample_B trace_1 trace_2 trace_3
1 S05-F13-P01:S05-F13-P01 S05-F13-P01    1.25    <NA>    <NA>
2 S05-F13-P01:S08-F10-P01 S08-F10-P01   0.227   0.227    <NA>
3 S05-F13-P01:S08-F11-P01 S08-F11-P01  -0.183  -0.183  -0.183
4 S05-F13-P01:S09-F66-P01 S09-F66-P01  -0.217  -0.217  -0.217
5 S05-F13-P01:S09-F67-P01 S09-F67-P01    <NA>   0.292   0.292
6 S05-F13-P01:S09-F01-P01 S09-F01-P01    <NA>    <NA>  0.0418
7 S05-F13-P01:S09-F68-P01 S09-F68-P01    <NA>    <NA>  -0.314

特别是当你有三个以上的矩阵/数据框时,使用mergeReduce缩放比嵌套合并更好。

答案 1 :(得分:2)

您还可以使用基数为R merge的{​​{1}}方法进行完整加入。

all = TRUE

这里合并完成了w.r.t.所有常见列,即> merge(merge(aa,bb,all=TRUE),cc,all=TRUE) key.related.sheet sample_B trace_1 trace_2 type trace_3 1 S05-F13-P01:S05-F13-P01 S05-F13-P01 1.25 <NA> <NA> <NA> 2 S05-F13-P01:S08-F10-P01 S08-F10-P01 0.227 0.227 Unknown <NA> 3 S05-F13-P01:S08-F11-P01 S08-F11-P01 -0.183 -0.183 Unknown -0.183 4 S05-F13-P01:S09-F66-P01 S09-F66-P01 -0.217 -0.217 Unknown -0.217 5 S05-F13-P01:S09-F67-P01 S09-F67-P01 <NA> 0.292 Unknown 0.292 6 S05-F13-P01:S09-F01-P01 S09-F01-P01 <NA> <NA> <NA> 0.0418 7 S05-F13-P01:S09-F68-P01 S09-F68-P01 <NA> <NA> <NA> -0.314 key.related.sheet - 但这应该没问题,因为sample_B取决于sample_B

使用key.related.sheet,您可以获得与使用dplyr的Adams回答相同的输出。然后合并就在w.r.t完成。左侧和右侧加入伙伴中的by="key.related.sheet"key.related.sheet列同时出现在结果中(即,您的数据都是重复的)

答案 2 :(得分:1)

您可以将矩阵转换为data.frame并使用dplyr包中的full_join命令将它们连接在一起

library(dplyr)
for(i in c("aa","bb", "cc")) assign(i, data.frame(get(i)))
aa %>% full_join(bb, by="key.related.sheet") %>% full_join(cc,
by="key.related.sheet")

        key.related.sheet  sample_B.x trace_1  sample_B.y trace_2    type    sample_B trace_3
1 S05-F13-P01:S05-F13-P01 S05-F13-P01    1.25        <NA>    <NA>    <NA>        <NA>    <NA>
2 S05-F13-P01:S08-F10-P01 S08-F10-P01   0.227 S08-F10-P01   0.227 Unknown        <NA>    <NA>
3 S05-F13-P01:S08-F11-P01 S08-F11-P01  -0.183 S08-F11-P01  -0.183 Unknown S08-F11-P01  -0.183
4 S05-F13-P01:S09-F66-P01 S09-F66-P01  -0.217 S09-F66-P01  -0.217 Unknown S09-F66-P01  -0.217
5 S05-F13-P01:S09-F67-P01        <NA>    <NA> S09-F67-P01   0.292 Unknown S09-F67-P01   0.292
6 S05-F13-P01:S09-F68-P01        <NA>    <NA>        <NA>    <NA>    <NA> S09-F68-P01  -0.314
7 S05-F13-P01:S09-F01-P01        <NA>    <NA>        <NA>    <NA>    <NA> S09-F01-P01  0.0418

答案 3 :(得分:1)

两个嵌套合并并删除无关列

merge(merge(aa,bb[, -4], by=c("key.related.sheet", "sample_B") ,all=TRUE), 
      cc,  by=c("key.related.sheet", "sample_B") ,all=TRUE)

        key.related.sheet    sample_B trace_1 trace_2 trace_3
1 S05-F13-P01:S05-F13-P01 S05-F13-P01    1.25    <NA>    <NA>
2 S05-F13-P01:S08-F10-P01 S08-F10-P01   0.227   0.227    <NA>
3 S05-F13-P01:S08-F11-P01 S08-F11-P01  -0.183  -0.183  -0.183
4 S05-F13-P01:S09-F66-P01 S09-F66-P01  -0.217  -0.217  -0.217
5 S05-F13-P01:S09-F67-P01 S09-F67-P01    <NA>   0.292   0.292
6 S05-F13-P01:S09-F01-P01 S09-F01-P01    <NA>    <NA>  0.0418
7 S05-F13-P01:S09-F68-P01 S09-F68-P01    <NA>    <NA>  -0.314