I am trying to make a list and access it's cells later in R. I am new to R and have a Matlab background. These []
, [[]]
are really bugging me.
I tried reading the help and online but I still don't get it. In the following code c["var1"][1]
returns differently than c$"var"[1]
.
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"