Oliver Nakoinz, Lizzie Scholtus, Néhémie Strupler
What is control flow?
for()
certain number of repetitionswhile()
as long as a condition is metrepeat()
until we break the loopif()
run some code or notif() else()
run some code or another codeFor loops repeat code a certain times and use an internal iteration variable.
## [1] "Loop no.: 1"
## [1] "Loop no.: 2"
## [1] "Loop no.: 3"
## [1] "Loop no.: 4"
## [1] "Loop no.: 5"
While loops repeat code until a certain condition is met. Iteration variables, if required have to be installed manually.
Analyze Code!
## [1] "Loop no.: 1"
## [1] "Loop no.: 2"
## [1] "Loop no.: 3"
## [1] "Loop no.: 4"
Iteration variable and the variable for the condition need not to be the same.
## [1] "Loop no.: 1"
## [1] "Loop no.: 2"
## [1] "Loop no.: 3"
## [1] "Loop no.: 4"
## [1] "Loop no.: 5"
## [1] "Loop no.: 6"
## [1] "Loop no.: 7"
## [1] "Loop no.: 8"
## [1] "Loop no.: 9"
## [1] "Loop no.: 10"
## [1] "Loop no.: 11"
## [1] "Loop no.: 12"
## [1] "Loop no.: 13"
If allows for conditional code. The condition contains a logical value and can make use of logical operators: ==, !=, <, <=, >, >=
The terms can be combined with and &
and or |
.
Repeat repeats code until we break the loop with break
.
i <- 1
repeat{print(paste("Loop no.: ", i))
i <- i + 1
if(i < 5){next}
print(paste("Value: ", 2^i))
if(i > 10){break}
}
## [1] "Loop no.: 1"
## [1] "Loop no.: 2"
## [1] "Loop no.: 3"
## [1] "Loop no.: 4"
## [1] "Value: 32"
## [1] "Loop no.: 5"
## [1] "Value: 64"
## [1] "Loop no.: 6"
## [1] "Value: 128"
## [1] "Loop no.: 7"
## [1] "Value: 256"
## [1] "Loop no.: 8"
## [1] "Value: 512"
## [1] "Loop no.: 9"
## [1] "Value: 1024"
## [1] "Loop no.: 10"
## [1] "Value: 2048"
If Else tests for a condition. If the condition is True Code 1 is terminated if False execute Code 2.
## [1] "deff"
Functions are container, shortcuts or names for pieces of code. Variables can be passed on to the functions as parameters.
analyze Code!
sqrt()
expects a number as a parameter or operates on a vectormean()
expects a vector as parameter apply
and co.apply
is a function that runs other functions for every column or row of a matrix or dataframe. apply
is usually faster than a loop.
analyze Code!
Which approach do you prefer?
Tidyverse is a philosophy and a style of data sciences within the R ecosphere, initiated by Hadley Wickham (now at RStudio). Tidyverse includes R-packages partly as part of the meta-package tidyverse
. The following slides are mainly based on Wickham/Grolemund (2017): http://r4ds.had.co.nz/.
readr
(Wickham/Hester 2021) reads data into tibbles.
library(readr)
ceramics <- readr::read_delim("../2data/22archdata/keramik.csv",
";",
escape_double = FALSE,
trim_ws = TRUE)
head(ceramics)
id | Hoehe | Breite | Rechts | Hoch | Grab | Dat | Motive | Beifunde_ker_etc | Beifund_metall | gemeinsamMit |
---|---|---|---|---|---|---|---|---|---|---|
1 | 5.0 | 17.0 | 324 | 7209 | Brandgrab | Lt B | 1,0,0,0,1,0,1,0 | Flasche H23 | NA | NA |
2 | 5.0 | 18.2 | 316 | 7209 | Körpergrab | Lt C1 | 0,1,0,1,0,0,1,1 | Schale S, Flasche S, Flasche S | Hiebmesser, Bronzeanhänger | NA |
3 | 6.0 | 19.1 | NA | NA | NA | NA | 1,1,0,0,0,0,0,0 | NA | NA | 2 |
4 | 5.2 | 17.0 | 394 | 7187 | Körpergrab | Lt B | 1,0,0,0,0,0,0,1 | Flasche H50.2, Schale, Quarzit | Pfeilspitze | NA |
5 | 8.2 | 17.4 | 350 | 7186 | Körpergrab | LtB/C | 0,0,0,1,0,0,0,0 | Flasche H29, Schüssel, Schale | Knotenring, Knotenring, Armring, Messer | NA |
6 | 8.0 | 19.5 | 316 | 7209 | Körpergrab | Lt C1 | 0,1,0,1,0,0,0,1 | Flasche S H50.5 | NA | NA |
pull()
can be used to simplify tibblesThe pipe (Bache/Wickham 2020) is forwarding the output of a function to the next function.
id | Hoehe | Breite | Motive |
---|---|---|---|
1 | 5.0 | 17.0 | 1,0,0,0,1,0,1,0 |
2 | 5.0 | 18.2 | 0,1,0,1,0,0,1,1 |
3 | 6.0 | 19.1 | 1,1,0,0,0,0,0,0 |
4 | 5.2 | 17.0 | 1,0,0,0,0,0,0,1 |
5 | 8.2 | 17.4 | 0,0,0,1,0,0,0,0 |
6 | 8.0 | 19.5 | 0,1,0,1,0,0,0,1 |
Groesse | m |
---|---|
FALSE | 18.03333 |
TRUE | 25.31667 |
poorman
is a replacement for dplyr
without tidyverse-integration.|>
, which does not depend on a package(Wickham 2021)
id | Hoehe | Breite | Rechts | Hoch | Grab | Dat | m1 | m2 | m3 | m4 | m5 | m6 | m7 | m8 | Beifunde_ker_etc | Beifund_metall | gemeinsamMit |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 5.0 | 17.0 | 324 | 7209 | Brandgrab | Lt B | 1 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | Flasche H23 | NA | NA |
2 | 5.0 | 18.2 | 316 | 7209 | Körpergrab | Lt C1 | 0 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | Schale S, Flasche S, Flasche S | Hiebmesser, Bronzeanhänger | NA |
3 | 6.0 | 19.1 | NA | NA | NA | NA | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | NA | NA | 2 |
4 | 5.2 | 17.0 | 394 | 7187 | Körpergrab | Lt B | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | Flasche H50.2, Schale, Quarzit | Pfeilspitze | NA |
5 | 8.2 | 17.4 | 350 | 7186 | Körpergrab | LtB/C | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | Flasche H29, Schüssel, Schale | Knotenring, Knotenring, Armring, Messer | NA |
6 | 8.0 | 19.5 | 316 | 7209 | Körpergrab | Lt C1 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 1 | Flasche S H50.5 | NA | NA |
Analyze Code!
id | key | value |
---|---|---|
1 | Hoehe | 5 |
2 | Hoehe | 5 |
3 | Hoehe | 6 |
4 | Hoehe | 5.2 |
5 | Hoehe | 8.2 |
6 | Hoehe | 8 |
id | Beifund_metall | Beifunde_ker_etc | Breite | Dat | gemeinsamMit | Grab | Hoch | Hoehe | m1 | m2 | m3 | m4 | m5 | m6 | m7 | m8 | Rechts |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | NA | Flasche H23 | 17.0 | Lt B | NA | Brandgrab | 7209 | 5.0 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 324 |
2 | Hiebmesser, Bronzeanhänger | Schale S, Flasche S, Flasche S | 18.2 | Lt C1 | NA | Körpergrab | 7209 | 5.0 | 0 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | 316 |
3 | NA | NA | 19.1 | NA | 2 | NA | NA | 6.0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | NA |
4 | Pfeilspitze | Flasche H50.2, Schale, Quarzit | 17.0 | Lt B | NA | Körpergrab | 7187 | 5.2 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 394 |
5 | Knotenring, Knotenring, Armring, Messer | Flasche H29, Schüssel, Schale | 17.4 | LtB/C | NA | Körpergrab | 7186 | 8.2 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 350 |
6 | NA | Flasche S H50.5 | 19.5 | Lt C1 | NA | Körpergrab | 7209 | 8.0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 1 | 316 |
ggplot2
(Wickham 2016) makes plots according to the “Grammar of Graphics”: data + geometry = graphic
plot
Objectgeom
Object (geometry)aes
Object (aesthetics)Load data from archdata
and calculate some mean and sums values.
calculate the sum of all numbers from 15 to 75 by
sum()
function andWrite a function, which divides a number a by a number b.
Calculate all length by high values (ore similar numbers) for the fibulae from archdata
by
apply()
,write a loop converting the long-form data to wide-form