[R] Match 2 vectors

Kang Min ngokangmin at gmail.com
Fri May 28 07:26:54 CEST 2010


I have 2 dataframes of unequal length, and I would like to match a
factor to them so that both dataframes will have the same number of

# create the 2 dataframes with unequal length
data1 <- data.frame(letters, 1:26)[-c(5,10,19:21),]
data2 <- data.frame(letters, 1:26)[-c(6,9,15:18),]

data2a <- match(data1[,1], data2[,1])
data2b <- data2[data2a,]

When I match data1 to data2, and combine the data2a vector to the
original data2, I'm still missing some rows. I need to get the 26
rows, and preferably with the first column displaying all the levels.
In data2b the mismatches show up as NA in the whole row.

Kang Min

More information about the R-help mailing list