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

后端 未结 7 1382
深忆病人
深忆病人 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:19

    The answer is simple. Well, kind of.

    The bottom line is that you should avoid confusion. Technically there is no reason to give your variables proper names, but it makes your code easier to read.

    Imagine having a line of code containing something like data()[1] or similar (this line probably doesn't make sense, but it's only an example): although it is clear to you now that you're using function data here, a reader who noticed there being a data.frame named data there, may be confused.

    And if you're not altruisticly inclined, remember that the reader could be you in half a year, trying to figure out what you were doing with 'that old code'.

    Take it from a man who has learned to use long variable names and naming conventions: it pays back!

提交回复
热议问题