do.call()不喜欢基函数"c".有清单 [英] do.call() doesn't like base function "c" with a list
问题描述
我有一段较大的代码,但是我已经将问题缩小到了- 所以我想返回一个串联列表.
I have a larger section of code but I've narrowed down the problem to this - So I want to return a concatenated list.
do.call(c,"X")
Error in do.call(c, "X") : second argument must be a list
因此,在上面它抱怨SECOND参数不是列表.
So above it complains about the SECOND argument not being a list.
asimplelist=list(2,3,4)
class(asimplelist)
[1] "list"
do.call(c,asimplelist)
Error in do.call(c, asimplelist) :
'what' must be a function or character string
为什么不返回串联列表? C是合法函数,并且正在传递一个列表?
Why will this not return a concatenated list ? C is a legit function, and it's being passed a list?
args(do.call)
function (what, args, quote = FALSE, envir = parent.frame())
NULL
所以"what"是它抱怨的函数参数.
So "what" is the function argument it is complaining about.
推荐答案
我将从尼克·肯尼迪:
最好将c
用双引号引起来.
It might be better to put the c
in double quotes.
如果用户在全局环境中具有名为c
的非功能,则do.call(c, dates)
将失败,并显示错误"Error in do.call(c, list(1:3)) : 'what' must be a character string or a function"
.
If the user has a non-function named c
in the global environment, do.call(c, dates)
will fail with the error "Error in do.call(c, list(1:3)) : 'what' must be a character string or a function"
.
很显然,定义c
可能不是最佳实践,但是人们通常习惯做a <- 1; b <- 2; c <- 3
.
Clearly it may not be best practice to define c
, but it's quite common for people to do a <- 1; b <- 2; c <- 3
.
在大多数情况下,R在这种情况下仍然可以正常工作; c(1, 2)
仍然可以工作,但do.call(c, x)
无效.
For most purposes, R still works fine in this scenario; c(1, 2)
will still work, but do.call(c, x)
won't.
当然,如果用户已将c
重新定义为功能(例如c <- sum
),则do.call
将使用重新定义的功能.
Of course if the user has redefined c
to be a function (e.g. c <- sum
), then do.call
will use the redefined function.
这篇关于do.call()不喜欢基函数"c".有清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!