Now that we're more comfortable with variables, it's a good idea to discuss some of the variable naming rules in R.
- A valid variable name consists of letters, numbers and the dot (
.) or underline (
_) characters. The variable name must start with either a letter or a dot that is not immediately followed by a number. In other words, names such as
.55weight are not valid, whereas
.weight is valid.
- We cannot name a variable with any special keywords like
else, or any others that are reserved by the language as part of its built-in functionality. Thus, something like
if <- 5 will give you an error.
- R is case sensitive, so the variables
height are not the same!
Variable names should be meaningful. For example, if a variable is used for storing a person's height, you will probably call it
height and not
Sometimes you will want to have a few words in your variable name. R doesn't have an established naming convention for multiple-word variable names. R programmers use multiple conventions, e.g.: separating the individual words with an underscore (
salary_tanya) or dot (
salary.tanya), or using camelCase capitalization so the individual words stand out (
It's important to use a consistent naming convention throughout your code. Choose a naming convention and stick to it. Prominent R users, like Hadley Wickham, recommend using lowercase and separate words with underscores in variable names. That's the convention we'll use in our code.