[R] rbind with different columns
Antje
niederlein-rstat at yahoo.de
Tue Oct 20 15:57:36 CEST 2009
Hello there,
with the following dummy code I'd like to give you an idea how my data
looks like:
df1 <- data.frame(a = rnorm(10), b = rnorm(10))
df2 <- data.frame(a = rnorm(10), c = rnorm(10))
df3 <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10))
myList <- list(df1, df2, df3) # (myList is representing the data
structure I have to handle, it's read from single files with lapply)
In every list entry is a data.frame but the columns are only partially
the same. If I have exactly the same columns, I could do the following
command to combine my data:
do.call("rbind", myList)
but of course it does not work with differnt column names. Is there any
easy way to retrieve a combined table like this:
a b c
1 -0.54586587 -0.3607873 NA
2 1.10876842 1.1439414 NA
3 0.57357988 -1.2117743 NA
4 -1.40975759 -1.2390305 NA
5 0.03371093 -1.8860748 NA
6 -1.27682961 0.9990840 NA
7 -1.78715858 0.8400642 NA
8 -0.22663310 1.5224016 NA
9 0.45703787 0.0599217 NA
10 -1.21984635 1.1991689 NA
11 -2.58848301 NA 0.2394272
12 0.71155177 NA -0.7107332
13 -2.16440676 NA -0.1744845
14 1.33043121 NA 0.5951272
15 1.51034297 NA 0.1956956
16 1.00844947 NA 0.6726101
17 0.78693840 NA 1.2189904
18 0.68622170 NA 1.2230500
19 -1.09376863 NA 0.4267472
20 2.23647873 NA 0.7328574
21 -0.38144792 0.1532647 1.4824618
22 0.27078024 -0.4264737 0.1317450
23 1.10812086 1.2550117 0.1677935
24 0.14881701 -0.2928157 -1.4081529
25 -1.00635045 -0.7885968 -0.3502532
26 0.32024094 0.4681016 -1.5477557
27 0.82974710 0.2345186 -0.6572728
28 0.49608133 1.7463265 0.6493405
29 -0.33022738 1.9510503 -1.7930093
30 -0.62615365 0.7330671 -0.4032405
The only thing I can think about is checking the names of each list
entry and adding NA-columns before combining them.
Is there any other way to do so?
Antje
More information about the R-help
mailing list