add several variables to dataframe, based on vector

Posted by Andreas on Stack Overflow See other posts from Stack Overflow or by Andreas
Published on 2010-04-17T19:03:01Z Indexed on 2010/04/17 19:33 UTC
Read the original article Hit count: 227

Filed under:

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

© Stack Overflow or respective owner

Related posts about r