Skip to content

Commit

Permalink
differences for PR #52
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Aug 6, 2024
1 parent 5fe2165 commit 6fc44e6
Show file tree
Hide file tree
Showing 16 changed files with 376 additions and 376 deletions.
26 changes: 13 additions & 13 deletions 01-intro-to-r.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ An alternative solution is to create the folders using R command `dir.create()`.
In the console type:


```r
``` r
dir.create("data")
dir.create("data_output")
dir.create("documents")
Expand Down Expand Up @@ -197,7 +197,7 @@ We will however need to install the `here` package. To do so, please go to your
script and type:


```r
``` r
install.packages("here")
```

Expand All @@ -210,7 +210,7 @@ of installed packages. If not, you will need to install it by writing in
the script:


```r
``` r
install.packages('tidyverse')
```

Expand All @@ -231,7 +231,7 @@ execution. Thanks to this feature, you can annotate your code.
Let's adapt our script by changing the first lines into comments:


```r
``` r
# install.packages('here')
# install.packages('tidyverse')
```
Expand All @@ -243,7 +243,7 @@ Installing packages is not sufficient to work with them. You will need to load
them each time you want to use them. To do that you use `library()` command:


```r
``` r
# Load packages
library(tidyverse)
library(here)
Expand Down Expand Up @@ -275,7 +275,7 @@ It might be confusing, so let's see how it works. We will use the `here()` funct
from the `here` package. In the console, we write:


```r
``` r
here()
here('data')
```
Expand All @@ -292,7 +292,7 @@ We will save it in the `data/` folder, where the **raw** data should go.
In the script, we will write:


```r
``` r
# Download the data
download.file(
"https://bit.ly/geospatial_data",
Expand Down Expand Up @@ -321,7 +321,7 @@ will not cover these in the workshop.
You can use R as calculator, you can for example write:


```r
``` r
1 + 100
1 * 100
1 / 100
Expand All @@ -335,14 +335,14 @@ use them whenever we need to.
We using the assignment operator `<-`, like this:


```r
``` r
x <- 1 / 40
```

Notice that assignment does not print a value. Instead, we've stored it for later
in something called a variable. `x` variable now contains the value `0.025`:

```r
``` r
x
```

Expand All @@ -352,21 +352,21 @@ Our variable `x` can be used in place of a number in any calculation that expect
a number, e.g. when calculating a square root:


```r
``` r
sqrt(x)
```

Variables can be also reassigned. This means that we can assign a new value to
variable `x`:

```r
``` r
x <- 100
x
```

You can use one variable to create a new one:

```r
``` r
y <- sqrt(x) # you can use value stored in object x to create y
y
```
Expand Down
78 changes: 39 additions & 39 deletions 02-data-structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,33 @@ Note that vector data in the geospatial context is different from vector data ty
You can create a vector with a `c()` function.


```r
``` r
# vector of numbers - numeric data type.
numeric_vector <- c(2, 6, 3)
numeric_vector
```

```output
``` output
[1] 2 6 3
```

```r
``` r
# vector of words - or strings of characters- character data type
character_vector <- c('Amsterdam', 'London', 'Delft')
character_vector
```

```output
``` output
[1] "Amsterdam" "London" "Delft"
```

```r
``` r
# vector of logical values (is something true or false?)- logical data type.
logical_vector <- c(TRUE, FALSE, TRUE)
logical_vector
```

```output
``` output
[1] TRUE FALSE TRUE
```

Expand All @@ -99,21 +99,21 @@ logical_vector
The combine function, `c()`, will also append things to an existing vector:


```r
``` r
ab_vector <- c('a', 'b')
ab_vector
```

```output
``` output
[1] "a" "b"
```

```r
``` r
abcd_vector <- c(ab_vector, 'c', 'd')
abcd_vector
```

```output
``` output
[1] "a" "b" "c" "d"
```

Expand Down Expand Up @@ -141,27 +141,27 @@ A common operation you want to perform is to remove all the missing values
(in R denoted as `NA`). Let's have a look how to do it:


```r
``` r
with_na <- c(1, 2, 1, 1, NA, 3, NA ) # vector including missing value
```

First, let's try to calculate mean for the values in this vector

```r
``` r
mean(with_na) # mean() function cannot interpret the missing values
```

```output
``` output
[1] NA
```

```r
``` r
# You can add the argument na.rm=TRUE to calculate the result while
# ignoring the missing values.
mean(with_na, na.rm = T)
```

```output
``` output
[1] 1.6
```

Expand All @@ -171,22 +171,22 @@ For this you need to identify which elements of the vector hold missing values
with `is.na()` function.


```r
``` r
is.na(with_na) # This will produce a vector of logical values,
```

```output
``` output
[1] FALSE FALSE FALSE FALSE TRUE FALSE TRUE
```

```r
``` r
# stating if a statement 'This element of the vector is a missing value'
# is true or not

!is.na(with_na) # The ! operator means negation, i.e. not is.na(with_na)
```

```output
``` output
[1] TRUE TRUE TRUE TRUE FALSE TRUE FALSE
```

Expand All @@ -195,14 +195,14 @@ Now we need to retrieve the subset of the `with_na` vector that is not `NA`.
Sub-setting in `R` is done with square brackets`[ ]`.


```r
``` r
without_na <- with_na[ !is.na(with_na) ] # this notation will return only
# the elements that have TRUE on their respective positions

without_na
```

```output
``` output
[1] 1 2 1 1 3
```

Expand All @@ -224,22 +224,22 @@ Once created, factors can only contain a pre-defined set of values,
known as levels.


```r
``` r
nordic_str <- c('Norway', 'Sweden', 'Norway', 'Denmark', 'Sweden')
nordic_str # regular character vectors printed out
```

```output
``` output
[1] "Norway" "Sweden" "Norway" "Denmark" "Sweden"
```

```r
``` r
# factor() function converts a vector to factor data type
nordic_cat <- factor(nordic_str)
nordic_cat # With factors, R prints out additional information - 'Levels'
```

```output
``` output
[1] Norway Sweden Norway Denmark Sweden
Levels: Denmark Norway Sweden
```
Expand All @@ -251,19 +251,19 @@ This can come in handy when performing statistical analysis.
You can inspect and adapt levels of the factor.


```r
``` r
levels(nordic_cat) # returns all levels of a factor vector.
```

```output
``` output
[1] "Denmark" "Norway" "Sweden"
```

```r
``` r
nlevels(nordic_cat) # returns number of levels in a vector
```

```output
``` output
[1] 3
```

Expand All @@ -281,7 +281,7 @@ displayed in a plot or which category is taken as a baseline in a statistical mo
You can reorder the categories using `factor()` function. This can be useful, for instance, to select a reference category (first level) in a regression model or for ordering legend items in a plot, rather than using the default category systematically (i.e. based on alphabetical order).


```r
``` r
nordic_cat <- factor(
nordic_cat,
levels = c(
Expand All @@ -295,7 +295,7 @@ nordic_cat <- factor(
nordic_cat
```

```output
``` output
[1] Norway Sweden Norway Denmark Sweden
Levels: Norway Denmark Sweden
```
Expand All @@ -306,7 +306,7 @@ There is more than one way to reorder factors. Later in the lesson,
we will use `fct_relevel()` function from `forcats` package to do the reordering.


```r
``` r
library(forcats)

nordic_cat <- fct_relevel(
Expand All @@ -320,7 +320,7 @@ nordic_cat <- fct_relevel(
nordic_cat
```

```output
``` output
[1] Norway Sweden Norway Denmark Sweden
Levels: Norway Denmark Sweden
```
Expand All @@ -332,11 +332,11 @@ it shows the underlying values of each category.
You can also see the structure in the environment tab of RStudio.


```r
``` r
str(nordic_cat)
```

```output
``` output
Factor w/ 3 levels "Norway","Denmark",..: 1 3 1 2 3
```

Expand All @@ -351,15 +351,15 @@ outside of this set, it will become an unknown/missing value detonated by



```r
``` r
nordic_str
```

```output
``` output
[1] "Norway" "Sweden" "Norway" "Denmark" "Sweden"
```

```r
``` r
nordic_cat2 <- factor(
nordic_str,
levels = c("Norway", "Denmark")
Expand All @@ -370,7 +370,7 @@ nordic_cat2 <- factor(
nordic_cat2
```

```output
``` output
[1] Norway <NA> Norway Denmark <NA>
Levels: Norway Denmark
```
Expand Down
Loading

0 comments on commit 6fc44e6

Please sign in to comment.