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?
sapply() function in R Language takes list, vector or data frame as input and gives output in vector or matrix. It is useful for operations on list objects and returns a list object of same length of original set.
The real reason for this is that sapply doesn't know what your function will return without calling it. In your case the function returns a logical , but since sapply is given an empty list, the function is never called. Therefore, it has to come up with a type and it defaults to list .
The sapply in R is a built-in function that applies a function to all the input elements. The sapply() method takes a list, vector, or data frame as an argument and returns a vector or matrix. The sapply() is an R wrapper class to lapply, with the difference being it returns a vector or matrix instead of a list object.
The sapply() function helps us in applying functions on a list, vector, or data frame and returns an array or matrix object of the same length. The sapply() function in the R Language takes a list, vector, or data frame as input and gives output in the form of an array or matrix object.
You could think of sapply and lapply as a for-loop that operates on seq_along(NOSJ) as an index vector.
for( i in seq_along(NOSJ) NOSJ[[i]] .... then use "[[" with the 3rd argument
So the first and second results would be:
> NOSJ[[1]][[1]]
$a
[1] 1
$b
[1] 1
> NOSJ[[2]][[1]]
$a
[1] 1
$b
[1] 1
The difference between sapply
and lapply
is that sapply
attempts to use simply2array
to return a matrix or array if the dimensions of the returned values are all the same (as they are in this case when using 1
, 3
or 5
as the 3rd argument. Quite honestly I do not know why using 2,4,or 6 as the third argument does not return an atomic vector. I thought it should.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With