Variables and data types

We call variable to a symbolic name that refers to an object. They act as memory containers for storing data values and are created when you assign a value to them using the assignment operator <- or =. For example, x <- 5 assigns the integer value 5 to the variable x.

The value stored in a variable can change or vary throughout your program and can be any data type such as integers, strings, or lists. You will see different data types further in this section.

name <- "Jean Golding"
age <- 27
weight <- 76.4
Assigment operators

In R, both = and <- can be used as assignment operators, but they have some subtle differences in usage.

  • The = operator is a general assignment operator in R and it’s more commonly used in function arguments and within function calls. mean(x = c(1, 2, 3))
  • The <- operator is the traditional and preferred assignment operator in R and has a higher precedence than =, which can be important in some contexts. x <- 5

Chosing the correct name for a particular variable is an important task as a non-descriptive name (or worse, an incorrect name) will be very confusing for you and anyone reading your code. For instance, for a variable which contains a number representing a distance in miles, avoid shortened names like dm, distm or d and instead use a name like distance_miles. Remember, code will be written once but read many times so make it easy to read.

Variable names

When naming variables in R, there are specific rules to follow:

  • Variable names can include letters (both uppercase and lowercase), digits, periods (.) and underscores (_), but they cannot start with a digit or underscore. Examples of valid variable names include my_var and var_2; invalid examples would be 2var (starts with a digit) or my-var (contains a hyphen).
  • Variable names are case-sensitive, meaning myVariable, MyVariable, and MYVARIABLE would be considered different variables.