Can anyone explain why list1 and list2 below are not identical?
list1 <- list()
lev1 <- "level1"
lev2 <- "level2"
list1[[lev1]][[lev2]] <- 1
list2 <- list()
list2$level1$level2 <- 1
This is occurring because you are assigning a length 1 vector to a NULL atomic vector.
$<-is applied to a NULL
x, it first coerces
list(). This is what also happens with
[[<-if the replacement value
valueis of length greater than one: if
valuehas length 1 or 0,
xis first coerced to a zero-length vector of the type of
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 2 list2 <- list() list2$level1$level2 <- 1:2 list2 # $level1 # $level1$level2 #  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 2
The result from
$<- is always a list so I have omitted it here.