dspgs dspgs - 3 months ago 9
R Question

How to call an vector inside a list without explicitly referencing object's name?

I have a list mylist and I know how to call one of its object by its name

head(mylist$"26533")
[1] 39.67125 33.33558 33.75013 51.71748 47.86691 35.98055


But when I try to get the same result with using x,

x <- "26533"
head(mylist$x)


R tells me the result is NULL.
Can anyone tell me what is the problem?

Answer

We can use [[ to pass a variable to extract the list element

mylist[[x]]

If there are multiple elements in 'x', use [

mylist[x]
Comments