A very important concept in programming is variable assignment. In R there are several assignment operators available: <-
, <<-
, =
, ->
, ->>
. For the sake of simplicity we will use the <-
assignment operator throughout the tutorial.
Let us assign two numerical values to variables denoted as x
and y
:
x <- 25
y <- 11
R will not print out anything to the console. However, we now have assigned the value 25 to the variable x
and the value 11 to the variable y
. These variables are now stored in our interactive workspace, referred to as global environment. The idea of environments, by which a set of names is associated to a set of values is quite fundamental, however, a sound discussion on this topic is beyond the topic of this section. The companion website for Advanced R, a book by Hadley Wickham, offers a nice introduction on this topic (here).
We can ask R which variable are currently stored in our workspace by typing the ls()
command.
ls()
## [1] "x" "y"
We can reuse these values by simple using the variable names in our arithmetic calculations.
x
## [1] 25
y
## [1] 11
x + y
## [1] 36