1

I would like to access an element from a list but I got NULL

  > x <- list("b" = TRUE)
  > x
   $b
  [1] TRUE
  > x$b
 [1] TRUE
  > var=c("b","c")

I tried this:

  > x$var[1]
  NULL
Tpellirn
  • 660
  • 4
  • 11

1 Answers1

1

We can use [[ instead of $ as $ would try to literally search for var as the list name instead of the value stored in the object

x[[var[1]]]
#[1] TRUE
akrun
  • 874,273
  • 37
  • 540
  • 662