R - merge two data frames?

For two data frames a and b :

 > a abc 1 -0.2246894 -1.48167912 -1.65099363 2 0.5559320 -0.87898575 -0.15634590 3 1.8469466 -0.01487524 -0.53098215 4 -0.6875051 0.23880967 0.01824621 5 -0.6735163 0.75485292 0.44154092 > b ac 1 0.4287284 -0.3295925 2 0.5201492 0.3341251 3 -2.6355570 1.7916780 4 -1.3645337 1.3642276 5 -0.4954542 -0.6660001 

Is there an easy way to combine them to return a new form data frame:

 > new abc 1 -0.2246894 -1.48167912106676 -1.65099363 2 0.5559320 -0.878985746842256 -0.15634590 3 1.8469466 -0.0148752354840942 -0.53098215 4 -0.6875051 0.238809666690982 0.01824621 5 -0.6735163 0.754852923524198 0.44154092 6 0.4287284 NA -0.32959248 7 0.5201492 NA 0.33412510 8 -2.6355570 NA 1.79167801 9 -1.3645337 NA 1.36422764 10 -0.4954542 NA -0.66600006 

Essentially, I want to combine the data, match the headers, and insert NA for the positions in dataframe b where the header is missing.

Is there an easy way to do this?

+75
r concatenation dataframe
Nov 17 '11 at 15:02
source share
5 answers

You want rbind.

 b$b <- NA new <- rbind(a, b) 

rbind requires that data frames have the same columns.

The first row adds column b to data frame b.

results

 > a <- data.frame(a=c(0,1,2), b=c(3,4,5), c=c(6,7,8)) > a abc 1 0 3 6 2 1 4 7 3 2 5 8 > b <- data.frame(a=c(9,10,11), c=c(12,13,14)) > b ac 1 9 12 2 10 13 3 11 14 > b$b <- NA > b acb 1 9 12 NA 2 10 13 NA 3 11 14 NA > new <- rbind(a,b) > new abc 1 0 3 6 2 1 4 7 3 2 5 8 4 9 NA 12 5 10 NA 13 6 11 NA 14 
+143
Dec 03 2018-11-11T00:
source share

Try the plyr package:

 rbind.fill(a,b,c) 
+18
Aug 15 '13 at 14:40
source share

Here is a simple little function that will combine the two datasets after automatically determining which columns are missing from each and adding them with all NA s.

For some reason, this returns MUCH faster in large data sets than with the merge function.

 fastmerge <- function(d1, d2) { d1.names <- names(d1) d2.names <- names(d2) # columns in d1 but not in d2 d2.add <- setdiff(d1.names, d2.names) # columns in d2 but not in d1 d1.add <- setdiff(d2.names, d1.names) # add blank columns to d2 if(length(d2.add) > 0) { for(i in 1:length(d2.add)) { d2[d2.add[i]] <- NA } } # add blank columns to d1 if(length(d1.add) > 0) { for(i in 1:length(d1.add)) { d1[d1.add[i]] <- NA } } return(rbind(d1, d2)) } 
+6
Aug 23 '15 at 1:20
source share

you can use function

bind_rows(a,b)

from dplyr library

+5
Feb 15 '17 at 13:10
source share

You can use rbind , but in this case you need to have the same number of columns in both tables, so try the following:

 b$b<-as.double(NA) #keeping numeric format is essential for further calculations new<-rbind(a,b) 
+1
Dec 02 '13 at 19:16
source share



All Articles