In R, what exactly is the problem with having variables with the same name as base R functions?

Answer

There isn't really one. R will not normally search objects (non function objects) when looking for a function:

> mean(1:10)[1]5.5> mean <-1> mean(1:10)[1]5.5> rm(mean)> mean(1:10)[1]5.5

The examples shown by @Joris and @Sacha are where poor coding catches you out. One better way to write foo is:

foo <-function(x, fun){
    fun <- match.fun(fun)
    fun(x)}

Which when used gives:

> foo(1:10, mean)[1]5.5> mean <-1> foo(1:10, mean)[1]5.5

There are situations where this will catch you out, and @Joris's example with na.omit is one, which IIRC, is happening because of the standard, non-standard evaluation used in lm().

All r Questions

Ask your interview questions on r

Write Your comment or Questions if you want the answers on r from r Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---