Reputation: 1022
I have 3 dataframes that I would like to bind together by row but also retain the columns that each one has such that columns not present in one dataframe are just initialized to NA and added to the resultant dataframe. Since I may have many more columns than the ones provided in the example below, I can't hardcode them as I have been doing so far.
a=data.frame(v1=rnorm(10),v2=rnorm(10),v3=rnorm(10))
b=data.frame(v1=rnorm(10),v3=rnorm(10),v4=rnorm(10))
c=data.frame(v2=rnorm(10),v5=rnorm(10),v6=rnorm(10))
Desired output:
Dimensions of 30 by 6 with an output header of
v1 v2 v3 v4 v5 v6
0.0.. 0.0.. 0.0.. NA NA NA
0.0.. NA 0.0.. 0.0.. NA NA
NA 0.0.. NA NA 0.0.. 0.0..
etc.
How do I achieve this in a scaleable and efficient way?
Upvotes: 3
Views: 46
Reputation: 59355
This is likely to be faster.
library(data.table)
result <- rbindlist(list(a,b,c), fill=TRUE)
result[c(1:2,11:12,21:22),]
# v1 v2 v3 v4 v5 v6
# 1: -0.7789103 0.9362939 -1.3353714 NA NA NA
# 2: 1.7435594 -1.0624084 1.2827752 NA NA NA
# 3: -0.8456543 NA 0.6196773 -1.6647646 NA NA
# 4: -1.2504797 NA -1.2812387 0.9288518 NA NA
# 5: NA 1.1489591 NA NA 1.3822840 -1.8260830
# 6: NA -0.8424763 NA NA 0.1684902 0.9952818
Upvotes: 1
Reputation: 21621
Try:
library(dplyr)
bind_rows(a, b, c)
From the documentation:
When row-binding, columns are matched by name, and any values that don't match will be filled with NA.
Upvotes: 2