Hi, it seems a simple problem, but I can not find a clear way. I have a list: lst=list(m=c('a','b','c'),n=c('c','a'),l=c('a','bc'))> lst$m [1] "a" "b" "c" $n [1] "c" "a" $l [1] "a" "bc" how can I get list elements that include a given subset? for example, for given subset {'a','c'}, the answer should be 'm' and 'n'. thanks Yu [[alternative HTML version deleted]]
Tena koe Yu One possibility: lst[sapply(lst, function(x) length(x[x%in% c('a','c')])==2)] HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Yuan Jian > Sent: Thursday, 24 June 2010 1:35 a.m. > To: r-help at r-project.org > Subject: [R] list operation > > Hi, > > it seems a simple problem, but I can not find a clear way. > I have a list: > lst=list(m=c('a','b','c'),n=c('c','a'),l=c('a','bc')) > > lst > $m > [1] "a" "b" "c" > $n > [1] "c" "a" > $l > [1] "a"? "bc" > > how can I get list elements that?include a given?subset? for example, > for given subset {'a','c'},?the answer should be?'m' and 'n'. > > thanks > Yu > > > > [[alternative HTML version deleted]]
> lst <- list(m=c('a','b','c'),n=c('c','a'),l=c('a','bc'))> f <- function(list, set) vapply(lst, function(el)all(is.element(set, el)), FUN.VALUE=logical(1)) > # if you have an old version of R use as.logical(sapply(...)) > # instead of vapply(..., FUN.VALUE=logical(10) > i <- f(lst, c("a","c")) > i m n l TRUE TRUE FALSE > names(lst)[i] [1] "m" "n" > lst[i] $m [1] "a" "b" "c" $n [1] "c" "a" Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Yuan Jian > Sent: Wednesday, June 23, 2010 6:35 AM > To: r-help at r-project.org > Subject: [R] list operation > > Hi, > ? > it seems a simple problem, but I can not find a clear way. > I have a list: > lst=list(m=c('a','b','c'),n=c('c','a'),l=c('a','bc')) > > lst > $m > [1] "a" "b" "c" > $n > [1] "c" "a" > $l > [1] "a"? "bc" > > how can I get list elements that?include a given?subset? for > example, for given subset {'a','c'},?the answer should be?'m' and 'n'. > ? > thanks > Yu > > > > [[alternative HTML version deleted]] > >
Yuan - There may be faster ways, but names(lst)[sapply(lst,function(i)'a' %in% i && 'c' %in% i)] seems to do what you want. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Wed, 23 Jun 2010, Yuan Jian wrote:> Hi, > ? > it seems a simple problem, but I can not find a clear way. > I have a list: > lst=list(m=c('a','b','c'),n=c('c','a'),l=c('a','bc')) >> lst > $m > [1] "a" "b" "c" > $n > [1] "c" "a" > $l > [1] "a"? "bc" > > how can I get list elements that?include a given?subset? for example, for given subset {'a','c'},?the answer should be?'m' and 'n'. > ? > thanks > Yu > > > > [[alternative HTML version deleted]] > >
Try this: lst[colSums(mapply('%in%', list(set), lst)) == 2] On Wed, Jun 23, 2010 at 10:35 AM, Yuan Jian <jayuan2008@yahoo.com> wrote:> Hi, > > it seems a simple problem, but I can not find a clear way. > I have a list: > lst=list(m=c('a','b','c'),n=c('c','a'),l=c('a','bc')) > > lst > $m > [1] "a" "b" "c" > $n > [1] "c" "a" > $l > [1] "a" "bc" > > how can I get list elements that include a given subset? for example, for > given subset {'a','c'}, the answer should be 'm' and 'n'. > > thanks > Yu > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]