This answer brought up the question of how the ellipsis feature in R
handles empty arguments. Apparently an empty argument in ...
works sometimes (see lapply
version below) but not other times (see sapply
version). Here's the example:
lst <- list(x=matrix(1))
lapply(lst, "[", 1, )
# $x
# [1] 1
sapply(lst, "[", 1, )
# Error in lapply(X = X, FUN = FUN, ...) :
# argument is missing, with no default
From what I can tell, sapply
actually just reuses its ...
arguments when calling lapply
. So I don't understand why lapply
works but sapply
doesn't. Can anybody explain this behavior.
In the sapply
help it states that
sapply(*, simplify = FALSE, USE.NAMES = FALSE) is equivalent to lapply(*).
However, I get the same results as above for the following:
lapply(lst, "[", i=1, j=)
sapply(lst, "[", i=1, j=, simplify=FALSE, USE.NAMES=FALSE)
By the way, I know that just adding TRUE
would solve the issue in this case, but I'm more interested in why there is a difference, not how to solve it. I'm actually more surprised that it works for the lapply
case than that it doesn't for the sapply
one.