如何合并具有不同行数的两个矩阵

时间:2012-11-08 10:27:13

标签: r matrix dataframe

假设我有2个矩阵看起来像这样:

structure(c(0.315789473684211, 0.315789473684211, 0.315789473684211, 
0.315789473684211, 0.315789473684211, 0.315789473684211, 0.315789473684211, 
0.315789473684211, 0.315789473684211, 0.315789473684211, 0.315789473684211, 
0.315789473684211, 0.315789473684211, 0.315789473684211), .Dim = c(14L, 
1L), .Dimnames = list(c("ch5_98500001", "ch5_99500001", "ch5_100500001", 
"ch5_101500001", "ch5_102500001", "ch5_103500001", "ch5_104500001", 
"ch5_105500001", "ch5_106500001", "ch5_110500001", "ch5_111500001", 
"ch5_112500001", "ch5_113500001", "ch5_114500001"), "loh"))
                    loh
ch5_98500001  0.3157895
ch5_99500001  0.3157895
ch5_100500001 0.3157895
ch5_101500001 0.3157895
ch5_102500001 0.3157895
ch5_103500001 0.3157895

structure(c(0.315789473684211, 0.315789473684211, 0.315789473684211, 
0.315789473684211, 0.315789473684211, 0.315789473684211, 0.315789473684211, 
0.315789473684211, 0.315789473684211, 0.315789473684211, 0.315789473684211, 
0.315789473684211, 0.315789473684211, 0.315789473684211, 0.315789473684211, 
0.315789473684211, 0.315789473684211, 0.315789473684211, 0.315789473684211, 
0.315789473684211, 0.315789473684211, 0.315789473684211, 0.315789473684211, 
0.315789473684211, 0.315789473684211, 0.315789473684211, 0.315789473684211, 
0.315789473684211, 0.315789473684211, 0.315789473684211, 0.315789473684211, 
0.315789473684211, 0.315789473684211, 0.315789473684211, 0.315789473684211, 
0.315789473684211, 0.315789473684211, 0.368421052631579, 0.368421052631579, 
0.368421052631579, 0.368421052631579, 0.368421052631579, 0.368421052631579, 
0.368421052631579, 0.368421052631579, 0.368421052631579, 0.368421052631579, 
0.368421052631579, 0.368421052631579, 0.368421052631579, 0.368421052631579, 
0.368421052631579, 0.368421052631579, 0.368421052631579, 0.368421052631579, 
0.368421052631579, 0.368421052631579, 0.368421052631579, 0.368421052631579, 
0.368421052631579, 0.368421052631579, 0.368421052631579, 0.368421052631579, 
0.368421052631579, 0.368421052631579, 0.368421052631579, 0.368421052631579, 
0.368421052631579, 0.368421052631579, 0.368421052631579, 0.368421052631579, 
0.368421052631579, 0.368421052631579, 0.368421052631579, 0.368421052631579, 
0.368421052631579, 0.368421052631579, 0.368421052631579, 0.368421052631579, 
0.368421052631579, 0.368421052631579, 0.368421052631579, 0.368421052631579
), .Dim = c(83L, 1L), .Dimnames = list(c("ch8_20500001", "ch8_21500001", 
"ch8_22500001", "ch8_23500001", "ch8_24500001", "ch8_25500001", 
"ch8_26500001", "ch8_27500001", "ch8_28500001", "ch18_500001", 
"ch18_1500001", "ch18_2500001", "ch18_3500001", "ch18_4500001", 
"ch18_5500001", "ch18_6500001", "ch18_7500001", "ch18_8500001", 
"ch18_9500001", "ch18_10500001", "ch18_11500001", "ch18_12500001", 
"ch18_13500001", "ch18_14500001", "ch18_15500001", "ch18_16500001", 
"ch18_17500001", "ch18_18500001", "ch18_19500001", "ch18_20500001", 
"ch18_21500001", "ch18_24500001", "ch18_25500001", "ch18_26500001", 
"ch18_27500001", "ch18_28500001", "ch18_29500001", "ch18_30500001", 
"ch18_31500001", "ch18_32500001", "ch18_33500001", "ch18_34500001", 
"ch18_35500001", "ch18_36500001", "ch18_37500001", "ch18_38500001", 
"ch18_39500001", "ch18_40500001", "ch18_41500001", "ch18_42500001", 
"ch18_43500001", "ch18_44500001", "ch18_45500001", "ch18_46500001", 
"ch18_47500001", "ch18_48500001", "ch18_49500001", "ch18_50500001", 
"ch18_51500001", "ch18_52500001", "ch18_53500001", "ch18_54500001", 
"ch18_55500001", "ch18_56500001", "ch18_57500001", "ch18_58500001", 
"ch18_59500001", "ch18_60500001", "ch18_61500001", "ch18_62500001", 
"ch18_63500001", "ch18_64500001", "ch18_65500001", "ch18_66500001", 
"ch18_67500001", "ch18_68500001", "ch18_69500001", "ch18_70500001", 
"ch18_71500001", "ch18_72500001", "ch18_73500001", "ch18_74500001", 
"ch18_75500001"), "loss"))
                  loss
ch8_20500001 0.3157895
ch8_21500001 0.3157895
ch8_22500001 0.3157895
ch8_23500001 0.3157895
ch8_24500001 0.3157895
ch8_25500001 0.3157895
ch8_26500001 0.3157895
ch8_27500001 0.3157895
ch8_28500001 0.3157895
ch18_500001  0.3157895
ch18_1500001 0.3157895
ch18_2500001 0.3157895

我希望它们合并为1个data.frame或矩阵,以便我有一个概述,并且不需要在文件之间切换。但解决这个问题似乎比使用data.frame更难。

任何人都知道如何不解决不同行长的问题?

1 个答案:

答案 0 :(得分:1)

 merge(as.data.frame(loss), as.data.frame(loh), by="row.names") 

但它给出了空数据框,因为在两个匹配的矩阵中都没有行名称。你也可以

 merge(as.data.frame(loss), as.data.frame(loh), by="row.names", all=TRUE) 

对于多个数据帧,我会使用函数Reduce

 Reduce(merge, list(data1, ..., dataN)))

确保您将by参数也传递给merge