non-numeric_argument
non-numeric_argument

Reputation: 658

How to add a column from a subsetted dataframe to the original dataframe in R?

I want to add one column from a dataframe (which is a subset of anther dataframe) into the original dataframe. (I had to use the subset for another analysis). Please consider this example:

origdata <- data.frame(id=c(1:5), val=c("a", "b", "c", "d", "e"))
subdata <- origdata[origdata$id>3, ]
subdata$newvar <- factor(c(1, 2), levels=c(1, 2), labels=c("one", "two")) 
### Value for newvar derived from other analysis

Now I want to add the column containing newvar to the original dataframe and fill in NAs for all cases which were not part of the subset. My first idea was to use

origdata[origdata$id>3, ]$newvar <- subdata$newvar

which obviously does not work. Though, if I first define newvar as NA it works but I lose the all additional information gained by specifying it as a factor (as above):

origdata$newvar <- NA
origdata[origdata$id>3, ]$newvar <- subdata$newvar

Do you know an easy work-around for this? If possible, I do NOT want to use merge because I have large dataframes and merge is very time-consuming. Also, I want to keep newvar as a factor.

Upvotes: 2

Views: 185

Answers (1)

Ananta
Ananta

Reputation: 3711

it should work

origdata$newvar<-subdata[match(origdata$id,subdata$id),"newvar"]

Upvotes: 2

Related Questions