Skip to content


A bad idea in R: using variables named like existing functions

R has an amazing robustness to programming weirdness. To give you a first idea: you can call a function with missing arguments, with named or unnamed (or truncated-named!) arguments, with badly-ordered arguments (as long as you name them), or with truncated parameter values as long as they are unambiguous. For instance, all these work and give the very same result:

curve(expr=dexp(x, rate = 1, log = TRUE),from=0,to=5);
curve(expr=dexp(x, rat = 1, lo = T),fro=0,to=5);
curve(dexp(x, rate = 1, log = T),0,5);
curve(from=0,to=5,expr=dexp(x,log = T, rate = 1));

And it is even possible to create a variable which has the same name as an existing standard function (you can’t do that with user-defined functions, though). For instance, check out this sequence:

> max=5;
> max(8,7);
[1] 8
> max;
[1] 5

We define a variable called “max”, set it to 5, then use the max() function normally, then call our “max” variable.

Falling to this kind of lazy standards, though, isn’t a good idea, because problems can arise. For instance if you name a variable like a standard function, you won’t be able to use apply() on it any more:

> A=matrix(c(1,5,2,4),2,2);
> max=5;
> apply(A,2,max);
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'FUN' of mode 'function' was not found

Posted in programming, R (R-project).


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Gijs says

    Good contribution. Got the same issue, and now I know why! Thanks.

  2. Nigel says

    You’d have to have an encyclopaedic knowledge to R functions to name your variables without otherwise unwieldy names though. Strike one for R for the rest of us!



Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA