Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching data from unequal length data frames in r

Tags:

r

match

This seems like it should be really simple. Ive 2 data frames of unequal length in R. one is simply a random subset of the larger data set. Therefore, they have the same exact data and a UniqueID that is exactly the same. What I would like to do is put an indicator say a 0 or 1 in the larger data set that says this row is in the smaller data set.

I can use which(long$UniqID %in% short$UniqID) but I can't seem to figure out how to match this indicator back to the long data set

like image 762
Kerry Avatar asked Nov 29 '22 08:11

Kerry


1 Answers

Made same sample data.

long<-data.frame(UniqID=sample(letters[1:20],20))
short<-data.frame(UniqID=sample(letters[1:20],10))

You can use %in% without which() to get values TRUE and FALSE and then with as.numeric() convert them to 0 and 1.

long$sh<-as.numeric(long$UniqID %in% short$UniqID)
like image 182
Didzis Elferts Avatar answered Dec 05 '22 13:12

Didzis Elferts