[R] How to get a specific named element in a nested list
Ivan Calandra
ivan.calandra at uni-hamburg.de
Thu Nov 11 10:23:02 CET 2010
Hi,
A reproducible example would have been nice, but a correct code even
more (you forgot some commas)!
So if you meant this:
x <- list(
list(
list(df1,df2),
list(df3,
list(df4,df5)),
list(df6,df7)))
check str(x): (I removes the details of each df)
List of 1
$ :List of 3
..$ :List of 2
.. ..$ :'data.frame': 10 obs. of 2 variables: ## df1
.. ..$ :'data.frame': 10 obs. of 2 variables: ## df2
..$ :List of 2
.. ..$ :'data.frame': 10 obs. of 2 variables: ## df3
.. ..$ :List of 2
.. .. ..$ :'data.frame': 10 obs. of 2 variables: ## df4
.. .. ..$ :'data.frame': 10 obs. of 2 variables: ## df5
..$ :List of 2
.. ..$ :'data.frame': 10 obs. of 2 variables: ## df6
.. ..$ :'data.frame': 10 obs. of 2 variables: ## df7
And check x: (I removed the details again)
[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
df1
[[1]][[1]][[2]]
df2
[[1]][[2]]
[[1]][[2]][[1]]
df3
[[1]][[2]][[2]]
[[1]][[2]][[2]][[1]]
df4
[[1]][[2]][[2]][[2]]
df5
[[1]][[3]]
[[1]][[3]][[1]]
df6
[[1]][[3]][[2]]
df7
If you want to access them, you need to follow the structure of your
list, e.g.
for df1:
x[[1]][[1]][[1]]
for df5:
x[[1]][[2]][[2]][[2]]
You need to access them with indexes because the list elements are unnamed.
Now if you do:
x <- list(uppermost=list(
medium1=list(df1=df1,df2=df2),
medium2=list(df3=df3,
lowermost=list(df4=df4,df5=df5)),
medium3=list(df6=df6,df7=df7)))
You can access them with names (but you still need to follow the
structure, i.e. x[[df5]] cannot work):
For df1:
x$uppermost$medium1$df1 or
x[["uppermost"]][["medium1"]][["df1"]]
for df5:
x$uppermost$medium2$lowermost$df5 or
x[["uppermost"]][["medium2"]][["lowermost"]][["df5"]]
But I guess you can write a function that would "search" through the
structure of x for a given df (that has to be named then) and return it.
Maybe you have your reasons to have such a complicated list, but if it
were me, I would make it easier to understand and to access (i.e. less
nested, max 2 levels)
HTH,
Ivan
Le 11/11/2010 09:05, Friedericksen Hope a écrit :
> Hello,
>
> I have a nested named list structure, like the following:
>
> x <- list(
> list(
> list(df1,df2)
> list(df3,
> list(df4,df5))
> list(df6,df7)))
>
> with df1...d7 as data frames. Every data frame is named.
>
> Is there a way to get a specific named element in x?
>
> so, for example,
>
> x[[c("df5")]] gives me the data frame 5?
>
> Thank you in advance!
>
> Best,
> Friedericksen
>
> ______________________________________________
> 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.
>
--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
More information about the R-help
mailing list