A followup to this How to use `[[` and `$` as a function? question: I started playing a bit with the original setup (reduced the size from 10000 to 3 for simplicity)
JSON <- rep(list(x,y),3)
x <- list(a=1, b=1)
y <- list(a=1)
JSON <- rep(list(x,y),3)
sapply(JSON, "[[", "a")
[1] 1 1 1 1 1 1
sapply(JSON,"[[",'b')
[[1]]
[1] 1
[[2]]
NULL
[[3]]
[1] 1
[[4]]
NULL
[[5]]
[1] 1
[[6]]
NULL
sapply(JSON,'[[',1)
[1] 1 1 1 1 1 1
sapply(JSON,'[[',2)
Error in FUN(X[[2L]], ...) : subscript out of bounds
That I think I understand -- searching for "b" is different from demanding the existence of a second element. But then, I created a deeper list:
NOSJ<-rep(list(JSON),3)
sapply(NOSJ,'[[',1)
[,1] [,2] [,3]
a 1 1 1
b 1 1 1
sapply(NOSJ,'[[',2)
$a
[1] 1
$a
[1] 1
$a
[1] 1
And now my head's hurting. Can someone expand on what [[
(or its sapply
method) is doing here?