The Assignment Operator
In this lesson, we'll explore how to use the assignment operator to store a variable in a computer's memory. We'll go into the details of computer memory in a few lessons.
The assignment operator is denoted by the symbol:
<-
. Think of it in terms of math operators
we have already encountered: the addition operator
+
combines the numbers on its left and right,
while the division operator /
divides the number on
its left by the one on its right. Just as the order matters in division,
the order is crucial with the assignment operator.
Let's see this in action.
In the above code snippet, we assign the value 1 to a new variable, a, using the assignment operator. This operator consists of two characters: the leftward-pointing arrow followed by a dash. Its design is intuitive; it visually represents storing the value on its right into the variable on its left. To view the value stored in this, simply type the variable's name.
What is a variable?
We call the object a a variable, but why, what is a variable? A variable stores a modifiable value into computer memory. And an object is the most general kind of object that stores anything into computer memory. Let's get some examples of these. In the below code area: a and b are variables, and c and d are objects.
a <- 1 # a is a variable
b <- "hello" # b is a variable
c <- c(1, 2, 3) # c is an object
d <- data.frame(y = c("a", "b", "c")) # d is an object
In some programming languages, depending on how they are created, a variable or object can not be modified after it is created. In R, any variable or object can be modified. In the below code editor let's look at how we can modify - aka update - the value assigned to the variable a.