2020-01-15
This module will provide you with the fundamental skills in
basis for
One of the most widely used programming languages and an effective tool for (geospatial) data science
The lectures and practical sessions have been designed to follow the schedule below
Suggested reading
Further reading
Created in 1992 by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand
When values and operations are inputted in the Console, the interpreter returns the results of its interpretation of the expression
2
## [1] 2
"String value"
## [1] "String value"
# comments are ignored
R provides three core data types
TRUE
or FALSE
R provides a series of basic numeric operators
Operator | Meaning | Example | Output |
---|---|---|---|
+ | Plus | 5 + 2 |
7 |
- | Minus | 5 - 2 |
3 |
* |
Product | 5 * 2 |
10 |
/ | Division | 5 / 2 |
2.5 |
%/% | Integer division | 5 %/% 2 |
2 |
%% | Module | 5 %% 2 |
1 |
^ | Power | 5^2 |
25 |
5 + 2
## [1] 7
R provides a series of basic logical operators to test
Operator | Meaning | Example | Output |
---|---|---|---|
== | Equal | 5 == 2 |
FALSE |
!= | Not equal | 5 != 2 |
TRUE |
> (>=) | Greater (or equal) | 5 > 2 |
TRUE |
< (<=) | Less (or equal) | 5 <= 2 |
FALSE |
! | Not | !TRUE |
FALSE |
& | And | TRUE & FALSE |
FALSE |
| | Or | TRUE | FALSE |
TRUE |
5 >= 2
## [1] TRUE
Variables store data and can be defined
a_variable
)<-
1
)a_variable <- 1
The value of the variable can be invoked by simply specifying the identifier.
a_variable
## [1] 1
An algorithm or effective procedure is a mechanical rule, or automatic method, or programme for performing some mathematical operation (Cutland, 1980).
A program is a specific set of instructions that implement an abstract algorithm.
The definition of an algorithm (and thus a program) can consist of one or more functions
Programming languages usually provide pre-defined functions that implement common algorithms (e.g., to find the square root of a number or to calculate a linear regression)
Functions execute complex operations and can be invoked
sqrt(2)
## [1] 1.414214
round(1.414214, digits = 2)
## [1] 1.41
<-
sqrt_of_two <- sqrt(2) sqrt_of_two
## [1] 1.414214
round(sqrt_of_two, digits = 2)
## [1] 1.41
round(sqrt(2), digits = 2)
## [1] 1.41
When creating an identifier for a variable or function
a_variable
is different from a_VARIABLE
.
and _
A coding style is a way of writing the code, including
_
# Bad X<-round(sqrt(2),2) #Good sqrt_of_two <- sqrt(2) %>% round(digits = 2)
Study the Tidyverse Style Guid and use it consistently!
Libraries are collections of functions and/or datasets.
install.packages
library
install.packages("tidyverse")
library(tidyverse)
The meta-library Tidyverse contains many libraries, including stringr
.
R provides some basic functions to manipulate strings, but the stringr
library provides a more consistent and well-defined set
str_length("Leicester")
## [1] 9
str_detect("Leicester", "e")
## [1] TRUE
str_replace_all("Leicester", "e", "x")
## [1] "Lxicxstxr"
The Tidyverse also provide a clean and effective way of combining multiple manipulation steps
The pipe operator %>%
The two codes below are equivalent
%>%
round(sqrt(2), digits = 2)
## [1] 1.41
sqrt(2) %>% round(digits = 2)
## [1] 1.41
An introduction to R
In the practical session, we will see
More complex data types