Kevin Burnham Kevin Burnham - 2 months ago 9
R Question

What is the difference between [[]] and $ in list indexing?

Can anyone explain why

list1
and
list2
below are not identical?

list1 <- list()
lev1 <- "level1"
lev2 <- "level2"
list1[[lev1]][[lev2]] <- 1
list1
$level1
level2
1

list2 <- list()
list2$level1$level2 <- 1
list2
$level1
$level1$level2
[1] 1

Answer

This is occurring because you are assigning a length 1 vector to a NULL atomic vector.

From help(Extract) -

When $<- is applied to a NULL x, it first coerces x to list(). This is what also happens with [[<- if the replacement value value is of length greater than one: if value has length 1 or 0, x is first coerced to a zero-length vector of the type of value.

Change the assignment to ... <- 1:2 (or something other than a length 0 or 1 vector) and you will get the same result in both code blocks.

list1 <- list()
lev1 <- "level1"
lev2 <- "level2"
list1[[lev1]][[lev2]] <- 1:2
list1
# $level1
# $level1$level2
# [1] 1 2

list2 <- list()
list2$level1$level2 <- 1:2
list2
# $level1
# $level1$level2
# [1] 1 2

A simpler example of this, as mentioned by @alexis_laz in the comments, is just to begin with a NULL atomic vector and look at what happens.

x <- NULL
## assign a length 1 vector --> atomic result
x[["lev1"]] <- 1
x
# lev1 
#    1 

y <- NULL
## assign a length > 1 vector --> list result
y[["lev1"]] <- 1:2
y
# $lev1
# [1] 1 2

The result from $<- is always a list so I have omitted it here.