Suppose I've 8 tables. Let 6 columns be same in each of those tables but, among those 8 tables 5 of them has one extra column(whose column name is same in all 5, that means those 5 of them has in total of 7 columns). My question is how we will bind all 8 tables such that the other 3 tables also now consist of that extra column which the other 5 has. I hope the question is quite clear.
Asked
Active
Viewed 5,189 times
0
-
4Place the datasets in a `list` i.e. `lst <- list(df1, df2, ...df8); library(data.table);rbindlist(lst, fill=TRUE)` – akrun Nov 07 '16 at 11:50
1 Answers
2
You can use rbind.fill
from the plyr package for this:
library(plyr)
# df_list contains a list of all the csv files you read, e.g. using lapply(list_paths, read.csv)
df_list = list(data.frame(a = c(1,2), b = c(3,4)),
data.frame(a = c(4,5), b = c(6,3), c = c(20, 21)))
> do.call('rbind.fill', df_list)
a b c
1 1 3 NA
2 2 4 NA
3 4 6 20
4 5 3 21
or alternatively, use rbindlist
from data.table
as @akrun suggested. This is probably a lot faster for larger datasets.

Paul Hiemstra
- 59,984
- 12
- 142
- 149
-
Thanks Paul, but is there any other option in which you don't have to type everything and use just a loop, or some other methods. – Aman Burman Nov 07 '16 at 12:04
-
I'm not sure what you mean. Using rbind.fill does not seem to be a lot of code. – Paul Hiemstra Nov 07 '16 at 18:21
-
I got the problem solved, since it was only a matter of 8 tables, I inserted the missing column in the other 3 tables manually and then combined them. lst <- list(v1, v2, v3, v4, v5, v6, v7, v8,); vCombined <- rbindlist(lst, fill=TRUE) – Aman Burman Nov 08 '16 at 06:30