I am trying to make a list and access its cells later in R. These [], [[]] are really bugging me. I tried reading the help online but I don't understand why c["var1"][1] and c$"var"[1] are different.
What are the actual uses for these three notations [], [[]], $?
v <- vector("character", 5)
v[1] <- 'a'
v[2] <- 'a'
v[4] <- 'a'
v
# [1] "a" "a" "" "a" ""
c <- list(v, v)
names(c) <- c("var1", "var2")
c
# $var1
# [1] "a" "a" "" "a" ""
# $var2
# [1] "a" "a" "" "a" ""
c["var1"][1]
# $var1
# [1] "a" "a" "" "a" ""
c$"var1"[1]
# [1] "a"
c[['var1']][1]as the[is still aliststrto see the structure of an object. See for instancestr(c[1])opposed tostr(c[[1]]). No need of quotes when using$:c$var1works just fine.help("[")and section 6.1 of An Introduction to R?help("[")is really helpful...