问题描述
此答案提出了一个问题,即R
中的省略号功能如何处理空参数.显然,...
中的空参数有时可以工作(请参见下面的lapply
版本),而其他时候则不能(请参见sapply
版本).这是示例:
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
据我所知,sapply
实际上只是在调用lapply
时重用了其...
参数.所以我不明白为什么lapply
可以工作,但是sapply
不能.任何人都可以解释这种行为.
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.
在sapply
帮助中指出
但是,在以下方面,我得到的结果与上述相同:
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)
顺便说一句,我知道在这种情况下只添加TRUE
就可以解决问题,但是我对为什么存在差异而不是如何解决更感兴趣.实际上,令我惊讶的是,它适用于lapply
情况,而不适用于sapply
情况.
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.
推荐答案
由于我根本不是C大师,所以我只能向您展示内部表示sapply
和lapply
的方式之间的区别.也许其他人可以在此答案上有所建树.
Because I am not a C master, at all, I can only show you what the differences are between how sapply
and lapply
are represented internally. And maybe someone else can build on this answer a little bit.
对于sapply
:我们将查看 https://github.com/SurajGupta/r-source/blob/master/src/library/base/R/sapply.R
对于不幸的是: https ://github.com/SurajGupta/r-source/blob/master/src/library/base/R/lapply.R
这表明,尽管sapply
调用了lapply
,但同时也调用了simplify2array
,这可能是问题之一,但是我的直觉告诉我这不是问题.
This shows that although sapply
calls lapply
it then also calls simplify2array
which could be one of the issues, but my intuition is telling me that this isn't the issue.
lapply
最终使用了.Internal(lapply(X,FUN))
,最终在这里的C源代码中调用了do_lapply
: https://github.com/SurajGupta/r-source/blob/91aaa43312da6b0e6906ef221fd7756dc0459843/src/main/apply.c
lapply
ends up using .Internal(lapply(X,FUN))
which ends up calling do_lapply
within the C source code here: https://github.com/SurajGupta/r-source/blob/91aaa43312da6b0e6906ef221fd7756dc0459843/src/main/apply.c
我的猜测是,由于sapply
固有地将两个参数传递给SEXP args
,因此...
参数正尝试使用sapply(lst, "[",
之后的所有内容创建sapply
的SEXP args
,并且因为它无法弄清在1,
或j=
之前要做什么simplify=FALSE, USE.NAMES=FALSE
,它失败.虽然那只是我的猜测.而lapply
不需要将其他参数强制输入到SEXP args
中,因此它可以以其他方式处理1,
或j=
.
My guess is that because sapply
inherently passes two parameters into SEXP args
the ...
argument is trying to create the SEXP args
of sapply
with everything after sapply(lst, "[",
and because it can't figure out what to do with either 1,
or j=
before it gets tosimplify=FALSE, USE.NAMES=FALSE
, it fails. Although that would just be my guess. Whereas lapply
doesn't have the additional arguments being forced into SEXP args
so it can handle 1,
or j=
in a different way.
这篇文章,了解.Internal C函数的处理方式在R 中,可以很好地解释一些C语言的实质内容.
This post, Understanding how .Internal C functions are handled in R, does a very nice job explaining some of the nitty-gritty C stuff.
希望这可以在某种程度上解释这些差异,或者至少可以作为具有更高R-devel技能的人的起点.
Hope this helps explain the differences to some degree, or at least can serve as a starting point for someone with greater R-devel skills.
这篇关于传递空省略号参数时的不同行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!