Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Text values
14. Rules related to variable names


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 and .55weight are not valid, whereas .weight is valid.
  • We cannot name a variable with any special keywords like if, then, 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 and 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 h.

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

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.


We have written four variables in the template on the right. Correct all of them to be in accordance with the recommended R naming convention (e.g. lowercase and underscores separating individual words).