[R] Flexible rbind

Santosh Srinivas
Thu, 17 Mar 2011 07:33:21 -0000

Dear All,

I am trying to create a empty structure that I want to fill gradually
through the code.
I want to use something like rbind to create the basic structure first.

I am looking for a possibility to do an rbind where the columns names
dont match fully (but the missing columns can be defaulted to either
zero or n/a)  (my actual data has a lot of columns).
Please see the data frames below

I have the following data frame that is filled
> dput(d)
structure(list(type = structure(1:9, .Label = c("test1",
"test2",
"test3", "test4", "test5", "test6",
"test7", "test8", "test9"
), class = "factor"), meter = c(37.25122438, 58.65845732, 63.47108421,
94.76085162, 13.75013867, 8.520664878, 79.74623167, 62.16109819,
20.47806657), degree = c(2.884440333, 74.94067898, 32.64251152,
83.24820274, 58.36084794, 12.64490368, 4.428796741, 32.12824213,
97.83710088)), .Names = c("type", "meter",
"degree"), class "data.frame", row.names = c(NA,
-9L))

To the above data I want to append the following data to create an
empty structure for the new data
> dput(dNewTests)
structure(list(type = structure(1:9, .Label = c("test14",
"test15",
"test16", "test17", "test18", "test19",
"test20", "test21", "test22"
), class = "factor")), .Names = "type", class =
"data.frame", row.names = c(NA,
-9L))

rbind obviously throws the following error:

 rbind(d, dNewTests)
Error in rbind(deparse.level, ...) :
  numbers of columns of arguments do not match

Any way that I can default the missing columns to NA or some default
value like zero?

Also, I tried to go through the archives and tried merge_all. What am
I doing wrong wih the code below ?

require(reshape)
merge_all(d, dNewTests,by="type")

Error in fix.by(by.x, x) :
  'by' must specify column(s) as numbers, names or logical


Thanks,
Santosh