# [R] list operation

Joris Meys jorismeys at gmail.com
Thu Jun 24 00:38:12 CEST 2010

```Another variation on the same theme :

lst=list(m=c('a','b','c'),n=c('c','a'),l=c('a','bc'))
set <- c('a','c')

f <-function(lst,set) sapply(lst,function(x) sum(set %in% x)==length(set) )
i <- f(lst,set)
names(i[i])

Doesn't serve anybody but keeps my mind fresh.

For long lists, you might benefit from first calculating the length of
set, to avoid having to do that n times for a list of length n.

Cheers
Joris

On Wed, Jun 23, 2010 at 11:01 PM, Peter Alspach
<Peter.Alspach at plantandfood.co.nz> wrote:
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

```