This post is based on this question:(Iterate over list to get value by its name)
I'll reproduce it here:
test_list <- list(list("name"="A","property"=1),
list("name"="B","property"=2),
list("name"="C","property"=3))
The original OP found:
sapply(test_list, `$`, "property")
only return NULL
values. It gives correct results if when use [[
instead as commented by jogo:
> sapply(test_list, `$`, "property")
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
> sapply(test_list, `[[`, "property")
[1] 1 2 3
This also does not work:
> sapply(test_list, function(x, y) `$`(x, y), y = "property")
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
However, both $
and [[
works on a single element of test_list
:
> `$`(test_list[[1]], 'property')
[1] 1
> `[[`(test_list[[1]], 'property')
[1] 1
I wonder why $
and [[
behave differently when used as FUN=
argument for lapply/sapply.