I have a list of lists, each sublist has dozens of elements and I just want to extract one of them.
x = list(`1` = list(a=1,b=2),`2` = list(a=3,b=4))
There are many ways to accomplish this, but I want to be as clever as possible so I try
lapply(x,`$`,"b")
# $`1`
# NULL
#
# $`2`
# NULL
but this doesn't work despite the fact that
`$`(x[[1]],"b")
# [1] 2
mapply
does work like this:
mapply(`$`,x,MoreArgs=list(name="b"))
# 1 2
# 2 4
I can use lapply
like this:
lapply(x,getElement,"b")
# $`1`
# [1] 2
#
# $`2`
# [1] 4
But why doesn't lapply
work with $
the way it ought to?