Silly question, but, can I test to see if any value of list a is contained in list b without doing a loop? A loop is easy enough, but wanted to see if there was a cleaner way. By way of example: List 1: a, b, c, d, e, f, g List 2: z, y, x, w, v, u, b Return true, since both lists contain b List 1: a, b, c, d, e, f, g List 2: z, y, x, w, v, u, t Return false, since the lists have no mutual values -- View this message in context: http://r.789695.n4.nabble.com/Are-any-values-in-one-list-contained-within-a-second-list-tp2253637p2253637.html Sent from the R help mailing list archive at Nabble.com.
Jorge Ivan Velez
2010-Jun-13 18:25 UTC
[R] Are any values in one list contained within a second list
Hi GL, Tr this: # example 1 list1 <- list(letters[1:7]) list1 list2 <- list(c('z','y','x','w','v','u','b')) list2 mapply(function(x, y) any(x %in% y), list1, list2) # example 2 list2 <- list(c('z','y','x','w','v','u','t')) list2 mapply(function(x, y) any(x %in% y), list1, list2) HTH, Jorge On Sun, Jun 13, 2010 at 2:17 PM, GL <> wrote:> > Silly question, but, can I test to see if any value of list a is contained > in > list b without doing a loop? A loop is easy enough, but wanted to see if > there was a cleaner way. By way of example: > > List 1: a, b, c, d, e, f, g > > List 2: z, y, x, w, v, u, b > > Return true, since both lists contain b > > List 1: a, b, c, d, e, f, g > > List 2: z, y, x, w, v, u, t > > Return false, since the lists have no mutual values > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Are-any-values-in-one-list-contained-within-a-second-list-tp2253637p2253637.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
David Winsemius
2010-Jun-13 18:26 UTC
[R] Are any values in one list contained within a second list
On Jun 13, 2010, at 2:17 PM, GL wrote:> > Silly question, but, can I test to see if any value of list a is > contained in > list b without doing a loop? A loop is easy enough, but wanted to > see if > there was a cleaner way. By way of example: > > List 1: a, b, c, d, e, f, g > > List 2: z, y, x, w, v, u, b > > Return true, since both lists contain b > > List 1: a, b, c, d, e, f, g > > List 2: z, y, x, w, v, u, t > > Return false, since the lists have no mutual valueslength(intersect(List_1, List_2)) > 0>David Winsemius, MD West Hartford, CT
William Dunlap
2010-Jun-13 18:56 UTC
[R] Are any values in one list contained within a second list
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of GL > Sent: Sunday, June 13, 2010 11:18 AM > To: r-help at r-project.org > Subject: [R] Are any values in one list contained within a second list > > > Silly question, but, can I test to see if any value of list a > is contained in > list b without doing a loop?It is almost the same as your English description of the problem: any(a %in% b)> A loop is easy enough, but > wanted to see if > there was a cleaner way. By way of example: > > List 1: a, b, c, d, e, f, g > > List 2: z, y, x, w, v, u, bIt would be nicer if you used R syntax to create the datasets in your example. E.g., I'm guessing you mean a1 <- c("a","b","c","d","e","f","g") but since you didn't quote the strings and you called it a "list" you could mean a2 <- as.list(expression(a, b, c, d, e, f, g)) or, equivalently, a3 <- list(quote(a), quote(b), quote(c), quote(d), quote(e), quote(f), quote(g)) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > Return true, since both lists contain b > > List 1: a, b, c, d, e, f, g > > List 2: z, y, x, w, v, u, t > > Return false, since the lists have no mutual values > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Are-any-values-in-one-list-contained-within-a-second-list-tp2253637p2253637.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Phil Spector
2010-Jun-13 19:25 UTC
[R] Are any values in one list contained within a second list
I think the simplest way is to translate the English directly :-)> list1 = c('a','b','c','d','e','f','g') > list2 = c('z','y','x','w','v','u','b') > any(list2 %in% list1)[1] TRUE> list2 = c('z','y','x','w','v','u','t') > any(list2 %in% list1)[1] FALSE - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Sun, 13 Jun 2010, GL wrote:> > Silly question, but, can I test to see if any value of list a is contained in > list b without doing a loop? A loop is easy enough, but wanted to see if > there was a cleaner way. By way of example: > > List 1: a, b, c, d, e, f, g > > List 2: z, y, x, w, v, u, b > > Return true, since both lists contain b > > List 1: a, b, c, d, e, f, g > > List 2: z, y, x, w, v, u, t > > Return false, since the lists have no mutual values > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Are-any-values-in-one-list-contained-within-a-second-list-tp2253637p2253637.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >