# [R] Match 2 vectors

Kang Min ngokangmin at gmail.com
Fri May 28 12:11:18 CEST 2010

That's what I wanted, thanks!!

On May 28, 5:13 pm, Dennis Murphy <djmu... at gmail.com> wrote:
> Hi:
>
> On Thu, May 27, 2010 at 10:26 PM, Kang Min <ngokang... at gmail.com> wrote:
> > Hi,
>
> > 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
> > rows.
>
> > example:
> > # 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),]
>
> If this is your real problem, then an easy thing to do is
>
>     data1 <- data2 <- data.frame(letters[1:26])
>     data1[c(5,10,19:21), ] <- NA
>     data2[c(6, 9, 15:18), ] <- NA
>     data3 <- cbind(data1, data2)
>     names(data3, c('lett1', 'lett2'))
>     data3
>
> However, I suspect this is not your real problem, so let's add a variable to
> each of the data frames you posted and try again:
>
> data1 <- data.frame(letters, 1:26)[-c(5,10,19:21),]
> data2 <- data.frame(letters, 1:26)[-c(6,9,15:18),]
>
> data1\$x <- rpois(nrow(data1), 10)
> data2\$y <- rpois(nrow(data2), 5)
>
> # Now merge data1 and data2:
> (data3 <- merge(data1, data2, all = TRUE))
>
>    letters X1.26  x  y
> 1        a     1  7  1
> 2        b     2 13  3
> 3        c     3 10  5
> 4        d     4  4  9
> 5        e     5 NA  4
> 6        f     6 10 NA
> 7        g     7 15  4
> 8        h     8 10  6
> 9        i     9 12 NA
> 10       j    10 NA  3
> 11       k    11  8  6
> 12       l    12 12  3
> 13       m    13 10  3
> 14       n    14  7  8
> 15       o    15  6 NA
> 16       p    16  8 NA
> 17       q    17  6 NA
> 18       r    18  4 NA
> 19       s    19 NA  2
> 20       t    20 NA  6
> 21       u    21 NA  4
> 22       v    22 12  6
> 23       w    23 10  3
> 24       x    24  7  8
> 25       y    25  9  6
> 26       z    26 15  7
>
> To get rid of the second column, set data3[, 2] <- NULL.
>
> HTH,
> Dennis
>
>
>
>
>
> > 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.
>
> > Thanks.
> > Kang Min
>
> > ______________________________________________
> > R-h... at r-project.org mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help