\(MOSAIC_{data}\)

Block 0: Introduction to R, Part 1

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

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

Program

Block 0: Introduction to R

Very short introduction

What is R?

R is a language and environment for statistical computing and graphics

Three dimensions of R

Why R?

Seven levels of using R

  1. Asking a colleague to run R for you
  2. Applying build in functions
  3. Combining build in functions
  4. Writing own algorithms
  5. Writing own functions and objects
  6. Developing efficient code
  7. Writing own packages

Three styles of using R

  1. Freehand
  2. Script based
  3. Literate Programming

Freehand

- flexible
- impressive
- badly documented

Terminal

Script based

Texteditor

Scripts: Some rules

 ##################################################################
 ## Didactic R-Script for Modelling Summer School
 ## ===============================================================
 ## Project: Modelling Summer School
 ## Author: O. Nakoinz, C. Filet & F. Faupel
 ## Version: 01
 ## Date of last changes: 10.09.2018
 ## Data: some.data
 ## Author of data: author.data
 ## Purpose: didactic
 ## Content: 1. preparation, 2. data import, ...
 ## Description: The script include ...
 ## Licence data: -
 ## Licence Script: GPL (http://www.gnu.org/licenses/gpl-3.0.html)
 ##################################################################

Scripts: Some rules

# This is a comment
a <- c(3,6,7,4,9,7,3,3,3)       # This is a vector
median(a)                       # This is the "median" function
## [1] 4

Scripts: Some rules

Scripts: Some rules

Folder Structure

Literate Programming

RMarkdown

Rule No 1

Do not try remembering all details, focus on the most important aspects

Knowledge means, knowing where it is written. (attributed to Albert Einstein)

Educated is who knows where to find what he does not know. (attributed to Georg Simmel)

Reading Code

Calculating with R: oparators

5 + 3
## [1] 8
5 - 3
## [1] 2
5 * 3
## [1] 15
5 / 3
## [1] 1.666667
5 ^ 3
## [1] 125
5 %% 3
## [1] 2
5 %/% 3
## [1] 1
Operator Description
+ addition
- subtraction
* multiplication
/ division
^ or ** exponentiation
x %% y modulus (x mod y) 5 %% 2 is 1
x %/% y integer division 5 %/% 2 is 2

read and analyze code!

Calculating with R: oparators

4 == 6
## [1] FALSE
4 != 6
## [1] TRUE
4 < 6
## [1] TRUE
4 <= 6
## [1] TRUE
4 > 6
## [1] FALSE
4 >= 6
## [1] FALSE
!TRUE
## [1] FALSE
(4 < 6) | (4 > 6)
## [1] TRUE
(4 < 6) & (4 > 6)
## [1] FALSE
isTRUE(26*2>4)
## [1] TRUE
Operator Description
== exactly equal to
!= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
!x Not x
x y
x & y x AND y
isTRUE(x) test if X is TRUE

Calculating with R: functions

sqrt(25)
## [1] 5
sin(3.14)
## [1] 0.001592653
log(25)
## [1] 3.218876
log10(25)
## [1] 1.39794
abs(-345.3356)
## [1] 345.3356
round(2.43135)
## [1] 2
ceiling(2.5)
## [1] 3
floor(2.5)
## [1] 2

Calculating with R: functions

sin(3.14)
## [1] 0.001592653
#sin(3,14)
sin(c(3,14))
## [1] 0.1411200 0.9906074

R-objects: variables

x <- 5
x
## [1] 5
sqrt(x)
## [1] 2.236068
y <- 7
5 -> x

Working directory

wd_lin <- "/home/xxxx/xxxx"
wd_win <- "D:\\xxx\\xxxx"
# set(wd_lin)

RStudio

Exercise

Part I ‐ Markdown

Markdown

Markdown

Markdown

  1. numbered list item: 1. numbered list item

block quotes: > block quotes

a b c
1 2 3
2 3 4

| a| b| c|
|--:|--:|--:|
| 1| 2| 3|
| 2| 3| 4|

Markdown and RStudio

Loading the package makes it available with the RStudio Addins.

library(remedy)

YAML Header

A Markdown file starts with a YAML header.

---
title: "Untitled"
output: html_document
---

YAML Header

---
title: "Untitled"
author: '[Prof. Dr. Oliver Nakoinz](http://oliver.nakoinz.gitlab.io/OliverNakoinz/)'
date: "WS 2020/2021"
bibliography: ../7lib/71citations/lit.bib
csl: ../7lib/72csl/iso690-author-date-cs.csl
lang: de-DE
otherlangs: en-GB
output:
  slidy_presentation:
    highlight: tango
    pandoc_args:
    - --css
    - stycss/styles.css
    footer: "Oliver Nakoinz"
    df_print: kable
fontsize: 14pt
font-family: 'Helvetica'
widescreen: true
---

Bibtex

  @book{xie2018r,
  title     = {R Markdown: The Definitive Guide},
  author    = {Xie, Y. and Allaire, J.J. and Grolemund, G.},
  isbn      = {9781138359338},
  series    = {Chapman and Hall/CRC the R Series},
  url       = {https://bookdown.org/yihui/rmarkdown/},
  year      = {2018},
  publisher = {Taylor \& Francis, CRC Press}
}

Bibtex

library(bibtex)
plist_v <- c("base",
             "sp",
             "sf")
write.bib(plist_v,
          file = 'lit_packages')

RMarkdown

```{r code chunks name}

a <- c(2, 3, 4)

sum(a)

```

a <- c(2, 3, 4)
sum(a)
## [1] 9

Tables

Markdown

| a| b| c|
|--:|--:|--:|
| 1| 2| 3|
| 2| 3| 4|

a b c
1 2 3
2 3 4

R

df <- data.frame(a = c(1, 2),
                 b = c(2, 3),
                 c = c(3, 4))
knitr::kable(df)
a b c
1 2 3
2 3 4

RMarkdown

Exercise

Reading code

Objects & Functions

Data types

22
## [1] 22
TRUE
## [1] TRUE
2i + 2
## [1] 2+2i
"Text"
## [1] "Text"
as.raw(65)
## [1] 41
as.Date("2018-11-15")
## [1] "2018-11-15"

Data structures

Data structures

Data structures: vector

A vector contains an ordered set of elements of the same type.

Vectors are defined with the function c (for concatenate).

x <- c(3, 5, 2, 2, 5, 8, 5, 2)
y <- c(6, 7, 2, 4, 5, 2, 9, 1)

vector

Data structures: vector

Vectors can be defined as sequence and many functions are available for manipulating vectors.

read and analyze code!

x <- c(3, 5, 2, 2, 5, 8, 5, 2)
y <- c(6, 7, 2, 4, 5, 2, 9, 1)

z  <- seq(1:5)
z2 <- seq(along = y)
z3 <- rep(3, 5)
x_sort <- sort(x)
x_rev  <- rev(x)
x_revsort <- rev(sort(x))
x_sortdec <- sort(x,
                  decreasing = T)

Data structures: vector

Parts of vectors (and other structures) can be addressed with indices.

x <- c(3, 5, 2, 2, 5, 8, 5, 2)

x[4]
## [1] 2
x[c(2, 4)]
## [1] 5 2
x[3:6]
## [1] 2 2 5 8
x[x>4]
## [1] 5 5 8 5

Data structures: vector

Vectors can be passed as parameter to functions. Some functions return information on the vectors.

x <- c(3, 5, 2, 2, 5, 8, 5, 2)

min(x)
## [1] 2
range(x)
## [1] 2 8
length(x)
## [1] 8

Data structures: matrix

x <- c(3, 5, 2, 2, 5, 8, 5, 2)
y <- c(6, 7, 2, 4, 5, 2, 9, 1)

zx <- matrix(x, 2, 4)
zy <- matrix(y, 2, 4)
zx + zy
##      [,1] [,2] [,3] [,4]
## [1,]    9    4   10   14
## [2,]   12    6   10    3

Data structures: array

(rarely used)

a <- array(data =