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 write:
data <- data.frame(...)
df <- data.frame(...)
Now, the function data
loads data sets while the function df
computes the f density function.
Similarly, it is tempting to write:
a <- 1
b <- 2
c <- 3
This is considered bad form because the function c
will combine its arguments.
But: In that workhorse of R functions, lm
, to compute linear models, data
is used as an argument. In other words, data
becomes an explicit variable inside the lm
function.
So: If the R core team can use identical names for variables and functions, what stops us mere mortals?
The answer is not that R will get confused. Try the following example, where I explicitly assign a variable with the name c
. R doesn't get confused at all with the difference between variable and function:
c("A", "B")
[1] "A" "B"
c <- c("Some text", "Second", "Third")
c(1, 3, 5)
[1] 1 3 5
c[3]
[1] "Third"
The question: What exactly is the problem with having variable with the same name as base R function?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…