将两个数据帧合并为一个,同时保留原始行号

时间:2015-07-12 06:04:22

标签: r join

我正在努力解决一个非常简单的问题。我有两个数据帧组合成一个数据帧,同时保留其原始行名称。如果您对使用这两个数据框感兴趣:

> dput(cc)
structure(list(c = c(166.081273211195, 117.874627144804, 85.7050194973198, 
122.960746859139, 144.149802403233, 90.8034500957001, 89.5265981283352, 
77.8535718910714, 144.544005656701, 115.597165020403, 109.028391182666, 
89.4045716355402, 77.7944830105746, 69.3378920684953, 79.9094499459695, 
146.768077595585, 157.933946809176, 92.562729606313, 62.5081476457419, 
90.1081848285295, 111.830482369239, 111.596975757741, 99.2311075024839, 
145.204385556523, 99.0215341758211, 75.4302512245677, 92.0094563363458, 
77.7314808830408)), .Names = "c", row.names = c("26", "27", "28", 
"29", "35", "36", "37", "38", "39", "40", "46", "47", "48", "49", 
"50", "51", "52", "56", "57", "58", "59", "60", "61", "62", "69", 
"70", "71", "72"), class = "data.frame")

> dput(ccc)
structure(list(b = c(76.376257255471, 61.8314936138378, 62.769450181685, 
73.6356164203567, 111.690756826382, 76.9294523843767, 61.3534699857719, 
69.3647221333577, 83.9764878084258, 81.3800252294203, 69.5091780233591, 
87.3595961209547, 78.5074999563006, 74.4479256924594, 81.5920316281566, 
96.3417259554163, 75.4138056616399, 76.0553034201146, 95.1759950844736, 
81.3252467041995, 86.306305649635, 70.5626459312969, 72.7797520793756, 
119.49702877934, 123.268678343102, 88.0450051118928, 76.2139948860248, 
98.1496728839206, 126.396927030103, 146.058540478643, 115.7341525964, 
87.280600158726, 78.0274068331766, 122.817977752389, 142.491559175427, 
152.895839114334, 94.4932174696818, 117.167042165763, 85.5340971715004, 
101.480170738897, 117.759691799033, 128.998051359269, 98.3180491401911, 
84.5915489017958, 87.4927520958843, 75.5366495973031, 118.088343275321, 
121.375320935357, 94.7724147096235, 70.2266610201599, 123.158462686523, 
76.87408931845, 94.4365460662552, 105.952134808703, 76.821070196668
)), .Names = "b", row.names = c("1", "2", "3", "4", "5", "6", 
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
"18", "19", "20", "21", "22", "23", "24", "25", "30", "31", "32", 
"33", "34", "41", "42", "43", "44", "45", "53", "54", "55", "63", 
"64", "65", "66", "67", "68", "73", "74", "75", "76", "77", "78", 
"79", "80", "81", "82", "83"), class = "data.frame")

最简单的组合方式是

> c(cc$c, ccc$b)
 [1] 166.08127 117.87463  85.70502 122.96075 144.14980  90.80345  89.52660  77.85357 144.54401
[10] 115.59717 109.02839  89.40457  77.79448  69.33789  79.90945 146.76808 157.93395  92.56273
[19]  62.50815  90.10818 111.83048 111.59698  99.23111 145.20439  99.02153  75.43025  92.00946
[28]  77.73148  76.37626  61.83149  62.76945  73.63562 111.69076  76.92945  61.35347  69.36472
[37]  83.97649  81.38003  69.50918  87.35960  78.50750  74.44793  81.59203  96.34173  75.41381
[46]  76.05530  95.17600  81.32525  86.30631  70.56265  72.77975 119.49703 123.26868  88.04501
[55]  76.21399  98.14967 126.39693 146.05854 115.73415  87.28060  78.02741 122.81798 142.49156
[64] 152.89584  94.49322 117.16704  85.53410 101.48017 117.75969 128.99805  98.31805  84.59155
[73]  87.49275  75.53665 118.08834 121.37532  94.77241  70.22666 123.15846  76.87409  94.43655
[82] 105.95213  76.82107

但这样做会重新创建行号。是否有任何简单的功能可以在保持行名完整的同时进行组合?谢谢!

1 个答案:

答案 0 :(得分:0)

这些代码行应解决问题

z0 <- as.numeric(c(rownames(cc), rownames(ccc)))
z <- data.frame(c(cc$c, ccc$b))
row.names(z) <- z0
data.frame(z[order(as.numeric(row.names(z))),])