I am sure this is easy – but I can’t figure it out right now.
Basically: I have a long vector of variables:
names <- c("first","second", "third")
I have some data, and I now need to add the variables. I could do:
data$first <- NA
But since I have a long list, and I would like an automated solution. This doesn’t work.
for (i in 1:length(names)) (paste("data$", names[i],sep="") <- NA)
The reason I want this, is that I need to vertically merge to dataframes, where one doesn’t have all the variables it should have.
Thanks in advance
You can access column using a variable for the name by using brackets instead of $.
But you could take a look at rbind.fill() in the reshape package (or plyr).
Hope this helps,
Etienne