[R] Intersecting list vs rows in matrix

Erik Iverson eriki at ccbr.umn.edu
Tue Aug 10 23:02:01 CEST 2010



GL wrote:
> Know that if I have List_1 and List_2 that I can check to see if the
> intersect via the code below:
> 
> List _1: 
> a, b, c, d, e, f, g 
> List_2: 
> z, y, x, w, v, u, b 
> length(intersect(List_1, List_2)) > 0
> return = true
> 
> If instead I wanted to check a dataframe that is a "list of lists," how
> would I do that by record without looping?
> 
> List _1: 
> a, b, c, d, e, f, g 
> 
> List_2: 
> z, y, x, w, v, u, b 
> y, z, w, v, v, u, m
> z, y, x, a, b, c
> .
> .
> .
> 
> return
> true
> false
> true

*Please* use actual R code and objects to represent your examples, not
pseudo-code!  I believe you may have some confusion about the difference
between lists and vectors in R.  A data.frame is a list, but not a "list
of lists".

Since you haven't given a reproducible example, it's hard for me to know
exactly what you want.

Assume a data.frame df1 composed of all character data, and a character
vector cv to match against.

apply(df1, 1, function(x) length(intersect(x, cv)) > 0)

may do what you want.



More information about the R-help mailing list