0

This is probably easy for the cracks but I cannot figure it out or find the answer through a search. I have a list of a list and I would like to access the underlying elements with a variable so that I can loop through it but I get NULL back instead of the the expected answer "a".

x <- list(alpha = c(gamma=list("a", "b")), beta=c(1, 2)) # creating the example
names(x)                                                 # as expected
# [1] "alpha" "beta" 
names(x$alpha)
# [1] "gamma1" "gamma2"                                      # as expected
n <- names(x$alpha)
x$alpha
# $gamma1
# [1] "a"
# $gamma2
# [1] "b"
x$alpha$gamma1
# [1] "a"                                                    # "a" is what I am looking for
n[1]
# [1] "gamma1"
x$alpha$n[1]
# NULL                                                       # why not "a"?

Why does x$alpha$n[1] return NULL and not "a" and how can I loop through all list members with something like for (i in 1:10) print(x$alpha$n[i])?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Gecko
  • 354
  • 1
  • 10

0 Answers0