skorkmaz
skorkmaz

Reputation: 586

Merge data frames and include duplicate rows

I have several data frames, such as

>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

When I try to merge these two data frames, here is what I get:

> 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

I need to get 18 rows but instead I got 16, I have duplicate rows and I want to keep them in the data sets. How can I do that?

Upvotes: 1

Views: 2158

Answers (1)

Sandy Muspratt
Sandy Muspratt

Reputation: 32789

Edit Ignore this answer. merge gives the desired output.

rbind() will put the two data frames together keeping all rows from both in the new data frame. Also, you would probably get more responses and timely responses if you retag your question with r.

First, getting your two data sets into 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)

Upvotes: 1

Related Questions