5  Variable Assignment

You can store a value (e.g. 2) or an object (e.g. a data frame) in a variable in R. You can later call this variable’s name to easily access the value or the object that has been stored within this variable. For example, you can assign a value 2 to a variable my_var with the command.

<- is the assignment operator in R. It can be also replaced by =. Assigning new values leads to overwriting previously assigned values. Variables already defined are shown in the environment window. By entering the variable name, the values associated with the variable are automatically printed.

Exercise B

Q1
  1. Define a variable named new_var with value 100,000.
  2. Multiply my_var (defined in my notes as 4 now) by new_var.
Q2
  • Define a variable named my_uni with a string "Arcadia".

The value of a variable can take different types, including logical values/boolean TRUE or FALSE.

Warning

When naming your variables, you can use letters, numbers, dots, and underscores.

However, no variable names can start with a number or a dot followed by a number.