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
  • 444
  • 2
  • 9

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
  • 789,025
  • 32
  • 460
  • 575