How to Successfully Follow Naming Conventions in R - dummies

How to Successfully Follow Naming Conventions in R

By Andrie de Vries, Joris Meys

R is very liberal when it comes to names for objects and functions. This freedom is a great blessing and a great burden at the same time. Nobody is obliged to follow strict rules, so everybody who programs something in R can basically do as he or she pleases.

How to choose a correct name in R

Although almost anything is allowed when giving names to objects, there are still a few rules in R that you can’t ignore:

  • Names must start with a letter or a dot. If you start a name with a dot, the second character can’t be a digit.

  • Names should contain only letters, numbers, underscore characters (_), and dots (.). Although you can force R to accept other characters in names, you shouldn’t, because these characters often have a special meaning in R.

  • You can’t use the following special keywords as names:

    • break

    • else

    • FALSE

    • for

    • function

    • if

    • Inf

    • NA

    • NaN

    • next

    • repeat

    • return

    • TRUE

    • while

R is case sensitive, which means that, for R, lastname and Lastname are two different objects. If R tells you it can’t find an object or function and you’re sure it should be there, check to make sure you used the right case.

Choose names in R that add meaning to your code

When Joris was young, his parents bought a cute little lamb that needed a name. After much contemplation, he decided to call it Blacky. Never mind that the lamb was actually white and its name made everybody else believe that it was a dog; Joris thought it was a perfect name.

Likewise, calling the result of a long script Blacky may be a bit confusing for the person who has to read your code later on, even if it makes all kinds of sense to you. Remember: You could be the one who, in three months, is trying to figure out exactly what you were trying to achieve. Using descriptive names will allow you to keep your code readable.

Although you can name an object whatever you want, some names will cause less trouble than others. You may have noticed that none of the functions we’ve used until now are mentioned as being off-limits. That’s right: If you want to call an object paste, you’re free to do so:

> paste <- paste("This gets","confusing")
> paste
[1] "This gets confusing"
> paste("Don't","you","think?")
[1] "Don't you think?"

R will always know perfectly well when you want the vector paste and when you need the function paste(). That doesn’t mean it’s a good idea to use the same name for both items, though. If you can avoid giving the name of a function to an object, you should.

One situation in which you can really get into trouble is when you use capital F or T as an object name. You can do it, but you’re likely to break code at some point. Although it’s a very bad idea, T and F are too often used as abbreviations for TRUE and FALSE, respectively. But T and F are not reserved keywords.

So, if you change them, R will first look for the object T and only then try to replace T with TRUE. And any code that still expects T to mean TRUE will fail from this point on. Never use F or T, not as an object name and not as an abbreviation.