[R] how to index a list with a string?
Hi, everybody. I have a list obj L, sth like $`aaa` [1] "5753" if the string 'aaa' is a returned value of a function foo(). what is the right syntax form of L$foo() I'm new to R, thanks in advance. __ 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.
Re: [R] how to index a list with a string?
On Aug 26, 2009, at 11:35 PM, Ning Ma wrote: Hi, everybody. I have a list obj L, sth like $`aaa` [1] "5753" if the string 'aaa' is a returned value of a function foo(). what is the right syntax form of L$foo() What do you really want to achieve or to learn? Pretty much anything ending in () will be seen as a function and it does not appear that you have a function inside L. > L <-list() > L$aaa <- 5753 > L$aaa <- "5753" > L $aaa [1] "5753" Note that aaa, which is a valid object name, does not have surrounding back-quotes as in your question. And even if you created an aaa portion of L using backquotes they would not be displayed that way: > L <-list() > L$`aaa` <- "5753" > L $aaa [1] "5753" If you constructed an invalid object name within back-quotes you might see them later: > L <-list() > L$`5753` <- "5753" > L $`5753` [1] "5753" So what is your real question and what does str(L) report? -- David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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.
Re: [R] how to index a list with a string?
Tena koe Try either L[foo()] or L[[foo()]] These return subtly (or not so subtly depending on your point of view) different results. HTH Peter Alspach > -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Ning Ma > Sent: Thursday, 27 August 2009 3:35 p.m. > To: r-help@r-project.org > Subject: [R] how to index a list with a string? > > Hi, everybody. I have a list obj L, sth like > > $`aaa` > [1] "5753" > > if the string 'aaa' is a returned value of a function foo(). > what is the right syntax form of L$foo() > > I'm new to R, thanks in advance. > > __ > 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. > __ 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.
Re: [R] how to index a list with a string?
Peter Alspach wrote: Tena koe Try either L[foo()] or L[[foo()]] These return subtly (or not so subtly depending on your point of view) different results. which is quite important, hence read the documentation. [] returns a list of length 1 (or a vector of length 1 of type list) where [[]] returns the value of the corresponding list element. This also means that mutpiple indices are interpreted recursively in [[]] but will select the corresponding list elements in []. Best, Uwe Ligges HTH Peter Alspach -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Ning Ma Sent: Thursday, 27 August 2009 3:35 p.m. To: r-help@r-project.org Subject: [R] how to index a list with a string? Hi, everybody. I have a list obj L, sth like $`aaa` [1] "5753" if the string 'aaa' is a returned value of a function foo(). what is the right syntax form of L$foo() I'm new to R, thanks in advance. __ 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. __ 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. __ 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.