Reputation: 7840
I have a list with elements that have the same name "element".
li <- list(element=list(id=1, name="x"), element=list(id=2, name="y"))
And I want to get the "name" of the element with an "id" equals to X.
Firstly i thought that I could do it with :
li[[which(li$element$id == 1)]]$name
But the problem is that li$element
refers to the first element "element" of the list ...
> li$element
$id
[1] 1
$name
[1] "x"
So if I look for an id different from the id of the first element of my list it returns "interger(0)"
> which(li$element$id == 2)
integer(0)
Actually I do like this :
for (element in li) {
if(element$id == 2) {
name <- element$name
}
}
But I wonder if there is a better way to do it, or if I missed something with the basic list notions (acces to elements ...).
Thank you
Upvotes: 0
Views: 642