2.5: Variables
( \newcommand{\kernel}{\mathrm{null}\,}\)
selected template will load here
This action is not available.
( \newcommand{\kernel}{\mathrm{null}\,}\)
A variable is a symbol that stands for another value (just like “X” in algebra). We can create a variable by assigning a value to it using the <-
operator. If we then type the name of the variable R will print out its value.
> x <- 4
> x
[1] 4
The variable now stands for the value that it contains, so we can perform operations on it and get the same answer as if we used the value itself.
> x + 3
[1] 7
> x == 5
[1] FALSE
We can change the value of a variable by simply assigning a new value to it.
> x <- x + 1
> x
[1] 5
A note: You can also use the equals sign =
instead of the <-