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?
4 Answers
You can use do.call, like so:
do.call("rbind", list(DF1, DF2, DF3))
Note that second argument of do.call is a list.
The tidyverse approach is to use bind_rows() from the dplyr package:
bind_rows(DF1, DF2, DF3)
For performance gains try rbindlist from the data.table package eg.
rbindlist(list(DF1,DF2,DF3))
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