R is an extremely powerful tool, however, still it can be used just as a calculator. Write the commands directly into your console (by default this is the lower left window in your RStudio environment) or write down the commands in a new R Script in your editor (go to the upper left green plus sign and open a new R script; your empty script will appear in the upper left window by default). From your R script you can execute each line separately (place your cursor anywhere in this line or highlight parts you want to execute) by the shortcut Ctrl+Enter (Windows keyboard) or Cmd+Return (Mac keyboard).
Note that #
is a comment and will be ignored by R
# 1 + 1 ... the line will be ignored
We can do any type of arithmetic computation
100 + 3
## [1] 103
50 - 25
## [1] 25
13 / 2
## [1] 6.5
Note that there is an order of operations, also referred to as operator precedence, which corresponds to a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given mathematical expression.
For instance
10 + 3 * 2
## [1] 16
is not the same as
(10 + 3) * 2
## [1] 26
Exercise: Use the basic mathematical operations to calculate the point of intersect with the x-axis of the following linear function in R: \(f(x)=\frac{4}7x+5\)
### your code here
(-5) * (7 / 4)
## [1] -8.75
In R the following unary and binary operators are defined. They are listed in precedence groups, from highest to lowest.
\[\begin{array}{r|l} \text{::} \quad \text{:::}&\text{access variables in a namespace}\\ \$ \text{ @} & \text{component / slot extraction}\\ \text{[} \quad \text{[[} & \text{indexing}\\ \text{^} &\text{exponentiation (right to left)}\\ \text{- +}&\text{unary minus and plus}\\ \text{:}&\text{sequence operator}\\ \text{%any%}&\text{special operators (including %% and %/%)}\\ \text{* /}&\text{multiply, divide}\\ \text{+ -}&\text{(binary) add, subtract}\\ \text{< > <= >= == !=}&\text{ordering and comparison}\\ \text{!}&\text{negation}\\ \text{&} \quad \text{&&}&\text{and}\\ \text{|} \quad \text{||}&\text{or}\\ \text{~}& \text{as in formulae}\\ \text{->} \quad \text{->>}&\text{rightwards assignment}\\ \text{<-} \quad \text{<<-}&\text{assignment (right to left)}\\ \text{=}&\text{assignment (right to left)}\\ \text{?}&\text{help (unary and binary)}\\ \end{array}\]
The symbol *
means multiply, and ^
means
“to the power”, so this gives 2 times 10 squared, i.e. 200
2 * 10^2
## [1] 200
Exercise: Use your already learned knowledge to calculate the 3rd Root of 4096!
### your code here
4096 ^ (1 / 3)
## [1] 16
R knows about infinity (and minus infinity)
1 / 0
## [1] Inf
Undefined results take the value NaN
(“not a
number”)
0 / 0
## [1] NaN
Citation
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.
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.