49

I have 3 data sets that I want to rbind together. I have renamed my columns to be the same:

names(DF1) <- c("A", "B", "C")
names(DF2) <- c("A", "B", "C")
names(DF3) <- c("A", "B", "C")

They have each got different numbers of observations (34, 54, 23, respectively)

However, when I try with an rbind function, it returns the error:

total <- rbind(DF1, DF2, DF3)

Error in match.names(clabs, names(xi)) : names do not match previous names

From other answered questions the error should arise because of differently named columns, but I have checked and rechecked that they have been renamed the same.

I would like to end up with a total dataset with a total of 111 observations with column titles. I am a beginner to R, so many of the answers from other questions elude me. Would anyone be able to answer this in layman terms?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
user2305783
  • 499
  • 1
  • 4
  • 3
  • Are you sure they have the same number of columns? Try `str(DF1)` and so on for all your datasets to see. – RJ- Apr 22 '13 at 03:01
  • 1
    Thanks! Turns out I had an extra null column in one of my datasets, but now my problem is that some of my rows disapeared after the rbind. So when I did 'code'(rbind(DF1,DF2)) Instead of the 88 rows I was expecting I ended up with 70 when checking with 'code'(nrow(total)) – user2305783 Apr 22 '13 at 03:09
  • what do you get in `nrow(DF1)` and `nrow(DF2)` before your `rbind`? – RJ- Apr 22 '13 at 03:12

4 Answers4

88

You can use do.call, like so:

do.call("rbind", list(DF1, DF2, DF3))

Note that second argument of do.call is a list.

Community
  • 1
  • 1
CHP
  • 16,981
  • 4
  • 38
  • 57
27

The tidyverse approach is to use bind_rows() from the dplyr package:

bind_rows(DF1, DF2, DF3)
Jot eN
  • 6,120
  • 4
  • 40
  • 59
10

For performance gains try rbindlist from the data.table package eg.

rbindlist(list(DF1,DF2,DF3))
dpel
  • 1,954
  • 1
  • 21
  • 31
  • It's a very straightforward way. Moreover, you can specify the option `idcol` to create a new column with the name of original `DF`. – jgarces Sep 02 '22 at 15:06
6

This may help you:

You can use rbind.fill from plyr package (can be used even if column name is not the same)

Here is the example from dataset in optmatch package in R

library(optmatch)
 library(plyr)
data(nuclearplants)
x<-nuclearplants
data1<-as.data.frame(x$cost)
data1<-data1[1:20,]
data1<-as.data.frame(data1)
data2<-as.data.frame(x$date)
rbind.fill(data1,data2)

data1    x$date
1  460.05     NA
2  452.99     NA
3  443.22     NA
4  652.32     NA
5  642.23     NA
6  345.39     NA
7  272.37     NA
8  317.21     NA
9  457.12     NA
10 690.19     NA
11 350.63     NA
12 402.59     NA
13 412.18     NA
14 495.58     NA
15 394.36     NA
16 423.32     NA
17 712.27     NA
18 289.66     NA
19 881.24     NA
20 490.88     NA
21     NA  68.58
22     NA  67.33
23     NA  67.33
24     NA  68.00
25     NA  68.00
26     NA  67.92
27     NA  68.17
28     NA  68.42
29     NA  68.42
30     NA  68.33
31     NA  68.58
32     NA  68.75
33     NA  68.42
34     NA  68.92
35     NA  68.92
36     NA  68.42
37     NA  69.50
38     NA  68.42
39     NA  69.17
40     NA  68.92
41     NA  68.75
42     NA  70.92
43     NA  69.67
44     NA  70.08
45     NA  70.42
46     NA  71.08
47     NA  67.25
48     NA  67.17
49     NA  67.83
50     NA  67.83
51     NA  67.25
52     NA  67.83
Metrics
  • 15,172
  • 7
  • 54
  • 83