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

后端 未结 7 1381
深忆病人
深忆病人 2020-11-30 03:48

It seems to be generally considered poor programming practise to use variable names that have functions in base R with the same name.

For example, it is tempting to

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 04:17

    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().

    Several Answers have also conflated the T vs TRUE issue with the masking of functions issue. As T and TRUE are not functions that is a little outside the scope of @Andrie's Question.

提交回复
热议问题