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 scope 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 variables are currently stored in our workspace by typing the ls() command.

## [1] "x" "y"

We can reuse these values by simply using the variable names in our arithmetic calculations.

## [1] 25
## [1] 11
x + y
## [1] 36


Exercise: Calculate the points of intersect with the x-axis of the following function in R: \(f(x)=x^2+3x-4\). Remember that you need the (Quadratic formula) (or formerly called p-q-formula) for this purpose. Use variables to store the points of intersect!

### your code here
Show code
b <- 3
c <- -4
# solve quadratic formula y=ax^2+bx+c with a=1
# in p-q-formula: b=p and c=q
x1 <- -(b / 2) + sqrt((b / 2)^2 - c)
x2 <- -(b / 2) - sqrt((b / 2)^2 - c)
## [1] 1
## [1] -4



The E-Learning project SOGA-R was developed at the Department of Earth Sciences by Kai Hartmann, Joachim Krois and Annette Rudolph. You can reach us via mail by soga[at]zedat.fu-berlin.de.

Creative Commons License
You may use this project freely under the Creative Commons Attribution-ShareAlike 4.0 International License.

Please cite as follow: Hartmann, K., Krois, J., Rudolph, A. (2023): Statistics and Geodata Analysis using R (SOGA-R). Department of Earth Sciences, Freie Universitaet Berlin.