合并数据框并包含重复的行

时间:2012-04-26 08:59:30

标签: r merge dataframe

我有几个数据框,例如

>data1
     pearson     fisher      yates  mec
1 0.01141204 0.02857143 0.05777957 2.50
2 0.02609829 0.04761905 0.11203684 2.25
3 0.05280751 0.07619048 0.19670560 2.00
4 0.09742169 0.16666667 0.31998422 1.75
5 0.16754628 0.42857143 0.49015296 1.50
6 0.27332168 0.44444444 0.71500065 1.25
7 0.42919530 1.00000000 1.00000000 1.00
8 0.65790502 1.00000000 0.65790502 0.75
9 1.00000000 1.00000000 0.29184055 0.50

> data2
     pearson     fisher     yates  mec
1 0.02609829 0.04761905 0.1120368 2.25
2 0.05777957 0.20634921 0.2059032 2.50
3 0.11203684 0.24242424 0.3403557 2.25
4 0.19670560 0.52380952 0.5186050 2.00
5 0.31998422 0.52380952 0.7402693 1.75
6 0.49015296 1.00000000 1.0000000 1.50
7 0.71500065 1.00000000 0.7150007 1.25
8 1.00000000 1.00000000 0.4291953 1.00
9 0.65790502 1.00000000 0.6579050 0.75

当我尝试合并这两个数据框时,我得到的是:

> merge(data1, data2, all=TRUE)
      pearson     fisher      yates  mec
1  0.01141204 0.02857143 0.05777957 2.50
2  0.02609829 0.04761905 0.11203684 2.25
3  0.05280751 0.07619048 0.19670560 2.00
4  0.05777957 0.20634921 0.20590321 2.50
5  0.09742169 0.16666667 0.31998422 1.75
6  0.11203684 0.24242424 0.34035574 2.25
7  0.16754628 0.42857143 0.49015296 1.50
8  0.19670560 0.52380952 0.51860502 2.00
9  0.27332168 0.44444444 0.71500065 1.25
10 0.31998422 0.52380952 0.74026928 1.75
11 0.42919530 1.00000000 1.00000000 1.00
12 0.49015296 1.00000000 1.00000000 1.50
13 0.65790502 1.00000000 0.65790502 0.75
14 0.71500065 1.00000000 0.71500065 1.25
15 1.00000000 1.00000000 0.29184055 0.50
16 1.00000000 1.00000000 0.42919530 1.00

我需要获得18行,但我得到16行,我有重复的行,我想将它们保存在数据集中。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

修改忽略此答案。 merge给出了所需的输出。

rbind()将两个数据帧放在一起,保留新数据帧中的所有行。此外,如果您使用r重新提问,则可能会获得更多回复和及时回复。

首先,将您的两个数据集放入r

data1 = read.table(text = "
 pearson    fisher     yates      mec
 0.01141204 0.02857143 0.05777957 2.50
 0.02609829 0.04761905 0.11203684 2.25
 0.05280751 0.07619048 0.19670560 2.00
 0.09742169 0.16666667 0.31998422 1.75
 0.16754628 0.42857143 0.49015296 1.50
 0.27332168 0.44444444 0.71500065 1.25
 0.42919530 1.00000000 1.00000000 1.00
 0.65790502 1.00000000 0.65790502 0.75
 1.00000000 1.00000000 0.29184055 0.50", sep = "", header = TRUE)

data2 = read.table(text = "
 pearson     fisher     yates  mec
 0.02609829 0.04761905 0.1120368 2.25
 0.05777957 0.20634921 0.2059032 2.50
 0.11203684 0.24242424 0.3403557 2.25
 0.19670560 0.52380952 0.5186050 2.00
 0.31998422 0.52380952 0.7402693 1.75
 0.49015296 1.00000000 1.0000000 1.50
 0.71500065 1.00000000 0.7150007 1.25
 1.00000000 1.00000000 0.4291953 1.00
 0.65790502 1.00000000 0.6579050 0.75", sep = "", header = TRUE)

data.merge = rbind(data1, data2)