\(MOSAIC_{data}\)

Block 0: Introduction to R, Part 3

Oliver Nakoinz, Lizzie Scholtus, Néhémie Strupler

2022-07-11
License: CC BY-SA 4.0

Problem

plot(test_df[, 2:3],
     main  = "Testplot",
     lines(test_df[, 2:3])
points(test_df[1:3, 7:3],
       pch = 17,
       col = "redd",
       cex = 3)
text(test_df[, 2:3],
       labels = test_df[, 1],
       pos    = 4
       cex    = 1)

Answer

plot(test_df[, 2:3],
     main  = "Testplot",
     lines(test_df[, 2:3])
points(test_df[1:3, 7:3],
       pch = 17,
       col = "redd",
       cex = 3)
text(test_df[, 2:3],
       labels = test_df[, 1],
       pos    = 4
       cex    = 1)
plot(test_df[, 2:3],
     main  = "Testplot")
lines(test_df[, 2:3])
points(test_df[1:3, 2:3],
       pch = 17,
       col = "red",
       cex = 3)
text(test_df[, 2:3],
       labels = test_df[, 1],
       pos    = 4,
       cex    = 1)

What is control flow?

Control flow

Control flow

For

For loops repeat code a certain times and use an internal iteration variable.

for (i in 1:5){
    print(paste("Loop no.: ", i))
}
## [1] "Loop no.:  1"
## [1] "Loop no.:  2"
## [1] "Loop no.:  3"
## [1] "Loop no.:  4"
## [1] "Loop no.:  5"

While

While loops repeat code until a certain condition is met. Iteration variables, if required have to be installed manually.

Analyze Code!

i <- 1
while(i < 5){
    print(paste("Loop no.: ", i))
    i <- i + 1
}
## [1] "Loop no.:  1"
## [1] "Loop no.:  2"
## [1] "Loop no.:  3"
## [1] "Loop no.:  4"

While

Iteration variable and the variable for the condition need not to be the same.

i <- 1
a <- 0
while(a < 5000){
    a <- 2^i
    print(paste("Loop no.: ", i))
    i <- i + 1
}
## [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

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 |.

if(3 == 4){print("abc")}

Repeat

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

If Else tests for a condition. If the condition is True Code 1 is terminated if False execute Code 2.

if(3 == 4){print("abc")
    }else{print("deff")}
## [1] "deff"

Conditions

Write your own functions

Functions are container, shortcuts or names for pieces of code. Variables can be passed on to the functions as parameters.

analyze Code!

add <- function(a, b){
    c <- a + b
    return(c)
}
add(3, 5)
## [1] 8

Vector based methods: using vectors

x <- c(2, 4, 1, 5)
sqrt(x)
## [1] 1.414214 2.000000 1.000000 2.236068
mean(x)
## [1] 3

Vector based methods: 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!

df <- data.frame(a = c(1, 2, 3, 4, 5),
                 b = c(5, 4, 3, 2, 1),
                 c = c(3, 5, 3, 7, 5))
apply(df,
      1,         # 1 for row; or 2 for column indexing
      mean)
## [1] 3.000000 3.666667 3.000000 4.333333 3.666667
apply(df,
      2,
      mean)
##   a   b   c 
## 3.0 3.0 4.6

Which approach do you prefer?

Tidyverse

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/.

Tidyverse

library("tidyverse")

Tidy data

Tidy data