-4

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?

dspgs
  • 15
  • 2
  • 4
    Possible duplicate of [Select a data frame column using $ and the name of the column as a string in a variable](http://stackoverflow.com/questions/18222286/select-a-data-frame-column-using-and-the-name-of-the-column-as-a-string-in-a-v) and another [similar question](http://stackoverflow.com/questions/23717397/r-how-use-a-string-variable-to-select-a-data-frame-column-using-notation) – Jota Aug 25 '16 at 07:04

1 Answers1

-2

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

mylist[[x]]

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

mylist[x]
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 12
    Maybe because for some users "Do not downvote a valid answer rule" is becoming less relevant after seeing your mountain of answers on duplicated posts. Please make an effort to find duplicate target post before posting an answer. – zx8754 Aug 25 '16 at 07:54
  • 4
    @akrun as I said before, don't ask this question. I would like to keep my choices to myself. – zx8754 Aug 25 '16 at 08:25