do.call() doesn't like base function “c” with a list

亡梦爱人 提交于 2019-12-22 10:30:02

问题


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

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

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 

So "what" is the function argument it is complaining about.


回答1:


I will answer "stealing" my answer from this comment by Nick Kennedy:

It might be better to put the c in double quotes.

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".

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.

For most purposes, R still works fine in this scenario; c(1, 2) will still work, but do.call(c, x) won't.

Of course if the user has redefined c to be a function (e.g. c <- sum), then do.call will use the redefined function.



来源:https://stackoverflow.com/questions/42499099/do-call-doesnt-like-base-function-c-with-a-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!