Dear list,
I'd like to access the elements of a list within another list with the
help of a variable.
dict <- list( "24" = c(1,2,3,6,12,24,48,72,96,120,144,168,720),
"168" = c(1,12,24,48,72,96,120,144,168,336,504,672),
"8760" = c(1,24,168,730,4380,8760)
)
dict$"24" works fine, but
a <- "24"
dict$a
returns NULL
Unfortunately dict[a] does not work for me, because I see no possibility
to access the inner elements of list "24" in that case
i.e. dict[a][1] returns the whole list and not the first element.
What is the correct syntax to access those elements with the help of a
variable?
Thanks in advance
Antje
access sublists by a variable
4 messages · Antje Gruner, R. Michael Weylandt, David Winsemius +1 more
dict[[a]] ? `[[` Michael
On Wed, Nov 16, 2011 at 10:16 AM, Antje Gruner <ntj at allesjetzt.net> wrote:
Dear list, I'd like to access the elements of a list within another list with the help of a variable. dict <- list( ?"24" = c(1,2,3,6,12,24,48,72,96,120,144,168,720), ? ? ? ? ?"168" = c(1,12,24,48,72,96,120,144,168,336,504,672), ? ? ? ? "8760" = c(1,24,168,730,4380,8760) ? ? ? ?) dict$"24" works fine, but a <- "24" dict$a returns ?NULL Unfortunately dict[a] does not work for me, because I see no possibility to access the inner elements of list "24" in that case i.e. dict[a][1] returns the whole list and not the first element. What is the correct syntax to access those elements with the help of a variable? Thanks in advance Antje
______________________________________________ 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.
On Nov 16, 2011, at 10:16 AM, Antje Gruner wrote:
Dear list,
I'd like to access the elements of a list within another list with the
help of a variable.
dict <- list( "24" = c(1,2,3,6,12,24,48,72,96,120,144,168,720),
"168" = c(1,12,24,48,72,96,120,144,168,336,504,672),
"8760" = c(1,24,168,730,4380,8760)
)
dict$"24" works fine, but
a <- "24"
dict$a
I can't remember if this is a FAQ but it is certainly discussed on the page you would ge with ?"$" dict[[a]]
returns NULL
Unfortunately dict[a] does not work for me, because I see no possibility to access the inner elements of list "24" in that case i.e. dict[a][1] returns the whole list and not the first element. What is the correct syntax to access those elements with the help of a variable? Thanks in advance Antje
______________________________________________ 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.
David Winsemius, MD West Hartford, CT
Hi: Try dict[[a]] rather than dict$a, and read the section on lists in the Introduction to R manual to learn how to properly index them. HTH, Dennis
On Wed, Nov 16, 2011 at 7:16 AM, Antje Gruner <ntj at allesjetzt.net> wrote:
Dear list, I'd like to access the elements of a list within another list with the help of a variable. dict <- list( ?"24" = c(1,2,3,6,12,24,48,72,96,120,144,168,720), ? ? ? ? ?"168" = c(1,12,24,48,72,96,120,144,168,336,504,672), ? ? ? ? "8760" = c(1,24,168,730,4380,8760) ? ? ? ?) dict$"24" works fine, but a <- "24" dict$a returns ?NULL Unfortunately dict[a] does not work for me, because I see no possibility to access the inner elements of list "24" in that case i.e. dict[a][1] returns the whole list and not the first element. What is the correct syntax to access those elements with the help of a variable? Thanks in advance Antje
______________________________________________ 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.