[R] show equal entries in data.frame

David Winsemius dwinsemius at comcast.net
Wed Sep 7 14:59:08 CEST 2011


On Sep 7, 2011, at 8:15 AM, Martin Batholdy wrote:

> Hi,
>
> I have the following data-frame:
>
>
> x <- data.frame(first = c('a','c','k','b'), second =  
> c('b','k','a','j'), third = c('f','a','h','b'))
>
>  first second third
> 1     a      b     f
> 2     c      k     a
> 3     k      a     h
> 4     b      j     b
>
>
> Now I would like to see wether there are entries that exists in all  
> three columns.
>
> In the example data-frame this would be true for 'a' and 'b'
> (so the row-number of the element is not important).

Because of the way you constructed this data.frame, you have factors.

a.in.b <- with( x,  first[first %in% second])

a.in.b,in.c <-  a.in.b[a.in.b %in% x$third]

a.in.b.in.c
#[1] a b
#Levels: a b c k

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list