[R] subset list based on logical within element flag

Christos Hatzis christos.hatzis at nuverabio.com
Thu Mar 13 00:37:48 CET 2008


Try:

> names(which(my_list == 'Fred'))
[1] "name"

You can break down the two nested calls to figure out what is happening.

-Christos

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Srinivas Iyyer
> Sent: Wednesday, March 12, 2008 6:53 PM
> To: Benilton Carvalho; mwkimpel at gmail.com
> Cc: R Help
> Subject: Re: [R] subset list based on logical within element flag
> 
> Hi I have the smililar question.
> 
> I have a list:
> my_list <- list(name="Fred", wife="Mary", no.children=3, 
> child.ages=c(4,7,9)) 
> 
> 
> > my_list
> $name
> [1] "Fred"
> 
> $wife
> [1] "Mary"
> 
> $no.children
> [1] 3
> 
> $child.ages
> [1] 4 7 9
> 
> 
> Now I want to search "Fred" and get attribute of that value 
> which is 'name'. 
> 
> how do I get it. 
> 
> > match(my_list,"Fred")
> [1]  1 NA NA NA
> 
> my thinking stops right here... 
> 
> list operations in R is too tough to digest and there is not 
> good documentations. 
> 
> Could any one please help. 
> 
> thanks
> Srini
> 
> 
> 
> 
> 
> 
> 
> --- Benilton Carvalho <bcarvalh at jhsph.edu> wrote:
> 
> > gene.pair.tf.lst[sapply(gene.pair.tf.lst, "[[", "sig.cor")]
> > 
> > b
> > 
> > 
> > On Mar 12, 2008, at 5:24 PM, Mark W Kimpel wrote:
> > 
> > > I have a very long list that I'd like to subset
> > based on a logical
> > > value
> > > within each element. Example below. I'd like to
> > get just those list
> > > elements for further study whose $sig.cor slot is
> > TRUE. In this
> > > example,
> > > I'd only want element [[2]].
> > >
> > > Should be simple, I know. How can I do this?
> > Thanks, Mark
> > >
> > >> gene.pair.tf.lst
> > > [[1]]
> > > [[1]]$gene.pair
> > > [1] "Lgals1:Pxmp2"
> > >
> > > [[1]]$sig.cor
> > > [1] FALSE
> > >
> > >
> > > [[2]]
> > > [[2]]$gene.pair
> > > [1] "Lgals1:Pxmp2"
> > >
> > > [[2]]$sig.cor
> > > [1] TRUE
> > >
> > >
> > > [[3]]
> > > [[3]]$gene.pair
> > > [1] "Lgals1:Pxmp2"
> > >
> > > [[3]]$sig.cor
> > > [1] FALSE
> > > --
> > >
> > > Mark W. Kimpel MD  ** Neuroinformatics ** Dept. of
> > Psychiatry
> > > Indiana University School of Medicine
> > >
> > > 15032 Hunter Court, Westfield, IN  46074
> > >
> > > (317) 490-5129 Work, & Mobile & VoiceMail
> > > (317) 204-4202 Home (no voice mail please)
> > >
> > > mwkimpel<at>gmail<dot>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.
> > 
> > > ______________________________________________
> > 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.
> >
> 
> ______________________________________________
> 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.
> 
>



More information about the R-help mailing list