An introduction to R: Basics of Algorithmics in R No emie Becker, - - PowerPoint PPT Presentation

an introduction to r basics of algorithmics in r
SMART_READER_LITE
LIVE PREVIEW

An introduction to R: Basics of Algorithmics in R No emie Becker, - - PowerPoint PPT Presentation

An introduction to R: Basics of Algorithmics in R No emie Becker, Sonja Grath & Dirk Metzler nbecker@bio.lmu.de - grath@bio.lmu.de Winter semester 2017-18 Back to input files 1 Conditional execution 2 Loops 3 Executing a command


slide-1
SLIDE 1

An introduction to R: Basics of Algorithmics in R

No´ emie Becker, Sonja Grath & Dirk Metzler

nbecker@bio.lmu.de - grath@bio.lmu.de

Winter semester 2017-18

slide-2
SLIDE 2

1

Back to input files

2

Conditional execution

3

Loops

4

Executing a command from a script

slide-3
SLIDE 3

Back to input files

Contents

1

Back to input files

2

Conditional execution

3

Loops

4

Executing a command from a script

slide-4
SLIDE 4

Back to input files

Review on data frame

Generic functions: read.table() write.table()

slide-5
SLIDE 5

Back to input files

Review on data frame

Generic functions: read.table() write.table() Example 1: wghtcls ”smoker” lifespan ”3” 0 50.3 3 0 52.8

slide-6
SLIDE 6

Back to input files

Review on data frame

Generic functions: read.table() write.table() Example 1: wghtcls ”smoker” lifespan ”3” 0 50.3 3 0 52.8 riscfactor <- read.table("lifespan2.txt",header=TRUE)

slide-7
SLIDE 7

Back to input files

Review on data frame

Example 2: wghtcls,smoker,lifespan 3,0,50.3 3,0,52.8

slide-8
SLIDE 8

Back to input files

Review on data frame

Example 2: wghtcls,smoker,lifespan 3,0,50.3 3,0,52.8 riscfactor <- read.csv("lifespan.csv") riscfactor <- read.table("lifespan.csv",header=TRUE, sep=",", fill=TRUE)

slide-9
SLIDE 9

Back to input files

Review on data frame

Example 2: wghtcls,smoker,lifespan 3,0,50.3 3,0,52.8 riscfactor <- read.csv("lifespan.csv") riscfactor <- read.table("lifespan.csv",header=TRUE, sep=",", fill=TRUE) Example 3: weight class smoker lifespan 3 0 50.3 3 0 52.8 riscfactor <- read.table("lifespan3.txt",header=TRUE)

slide-10
SLIDE 10

Back to input files

Review on data frame

Example 2: wghtcls,smoker,lifespan 3,0,50.3 3,0,52.8 riscfactor <- read.csv("lifespan.csv") riscfactor <- read.table("lifespan.csv",header=TRUE, sep=",", fill=TRUE) Example 3: weight class smoker lifespan 3 0 50.3 3 0 52.8 riscfactor <- read.table("lifespan3.txt",header=TRUE) You have to change the first line of the file because of the space between weight and class.

slide-11
SLIDE 11

Back to input files

Factors

A variable (numeric or text) can be intended as a factor.

slide-12
SLIDE 12

Back to input files

Factors

A variable (numeric or text) can be intended as a factor. Example with text: x <- c("female","male","male","female","female")

slide-13
SLIDE 13

Back to input files

Factors

A variable (numeric or text) can be intended as a factor. Example with text: x <- c("female","male","male","female","female") levels(x)

slide-14
SLIDE 14

Back to input files

Factors

A variable (numeric or text) can be intended as a factor. Example with text: x <- c("female","male","male","female","female") levels(x) NULL

slide-15
SLIDE 15

Back to input files

Factors

A variable (numeric or text) can be intended as a factor. Example with text: x <- c("female","male","male","female","female") levels(x) NULL str(x)

slide-16
SLIDE 16

Back to input files

Factors

A variable (numeric or text) can be intended as a factor. Example with text: x <- c("female","male","male","female","female") levels(x) NULL str(x) chr [1:5] "female" "male" "male" "female" "female"

slide-17
SLIDE 17

Back to input files

Factors

A variable (numeric or text) can be intended as a factor. Example with text: x <- c("female","male","male","female","female") levels(x) NULL str(x) chr [1:5] "female" "male" "male" "female" "female" x <-factor(x)

slide-18
SLIDE 18

Back to input files

Factors

A variable (numeric or text) can be intended as a factor. Example with text: x <- c("female","male","male","female","female") levels(x) NULL str(x) chr [1:5] "female" "male" "male" "female" "female" x <-factor(x) levels(x) [1] "female" "male" str(x) Factor w/ 2 levels "female","male": 1 2 2 1 1

slide-19
SLIDE 19

Back to input files

Factors

Example with numbers: y <- rep(c(17,17,18),4); str(y) num [1:12] 17 17 18 17 17 18 17 17 18 17 ...

slide-20
SLIDE 20

Back to input files

Factors

Example with numbers: y <- rep(c(17,17,18),4); str(y) num [1:12] 17 17 18 17 17 18 17 17 18 17 ... summary(y) Min. 1st Qu. Median Mean 3rd Qu. Max. 17.00 17.00 17.00 17.33 18.00 18.00

slide-21
SLIDE 21

Back to input files

Factors

Example with numbers: y <- rep(c(17,17,18),4); str(y) num [1:12] 17 17 18 17 17 18 17 17 18 17 ... summary(y) Min. 1st Qu. Median Mean 3rd Qu. Max. 17.00 17.00 17.00 17.33 18.00 18.00 y <- factor(y); str(y) Factor w/ 2 levels "17","18": 1 1 2 1 1 2 1 1 2 1 ... summary(y) 17 18 8 4

slide-22
SLIDE 22

Back to input files

Back to input files

By default read.table() sets text variables as factors and not numerical variables.

slide-23
SLIDE 23

Back to input files

Back to input files

By default read.table() sets text variables as factors and not numerical variables. This can be changed by specifying the class of the columns. riscfactor <- read.table("lifespan2.txt",header=TRUE, colClasses=c("factor","numeric","numeric"))

slide-24
SLIDE 24

Back to input files

Back to input files

By default read.table() sets text variables as factors and not numerical variables. This can be changed by specifying the class of the columns. riscfactor <- read.table("lifespan2.txt",header=TRUE, colClasses=c("factor","numeric","numeric")) Or by changing the variables afterwards. riscfactor$wghtcls <- factor(riscfactor$wghtcls)

slide-25
SLIDE 25

Conditional execution

Contents

1

Back to input files

2

Conditional execution

3

Loops

4

Executing a command from a script

slide-26
SLIDE 26

Conditional execution

Logic rules in R

4 == 4 #Are both sides equal? [1] TRUE #TRUE is a constant in R

slide-27
SLIDE 27

Conditional execution

Logic rules in R

4 == 4 #Are both sides equal? [1] TRUE #TRUE is a constant in R 4 == 5 #Are both sides equal? [1] FALSE #FALSE is a constant in R

slide-28
SLIDE 28

Conditional execution

Logic rules in R

4 == 4 #Are both sides equal? [1] TRUE #TRUE is a constant in R 4 == 5 #Are both sides equal? [1] FALSE #FALSE is a constant in R 2 != 3 # ! is negation, != is ’not equal’

slide-29
SLIDE 29

Conditional execution

Logic rules in R

4 == 4 #Are both sides equal? [1] TRUE #TRUE is a constant in R 4 == 5 #Are both sides equal? [1] FALSE #FALSE is a constant in R 2 != 3 # ! is negation, != is ’not equal’ 3 != 3 3 <= 5 5 >= 2*2

slide-30
SLIDE 30

Conditional execution

Logic rules in R

4 == 4 #Are both sides equal? [1] TRUE #TRUE is a constant in R 4 == 5 #Are both sides equal? [1] FALSE #FALSE is a constant in R 2 != 3 # ! is negation, != is ’not equal’ 3 != 3 3 <= 5 5 >= 2*2 Caution: cos(pi/2) == 0

slide-31
SLIDE 31

Conditional execution

Logic rules in R

4 == 4 #Are both sides equal? [1] TRUE #TRUE is a constant in R 4 == 5 #Are both sides equal? [1] FALSE #FALSE is a constant in R 2 != 3 # ! is negation, != is ’not equal’ 3 != 3 3 <= 5 5 >= 2*2 Caution: cos(pi/2) == 0 [1] FALSE

slide-32
SLIDE 32

Conditional execution

Logic rules in R

4 == 4 #Are both sides equal? [1] TRUE #TRUE is a constant in R 4 == 5 #Are both sides equal? [1] FALSE #FALSE is a constant in R 2 != 3 # ! is negation, != is ’not equal’ 3 != 3 3 <= 5 5 >= 2*2 Caution: cos(pi/2) == 0 [1] FALSE cos(pi/2) [1] 6.123234e-17

slide-33
SLIDE 33

Conditional execution

Logic rules in R

TRUE & TRUE # & is the logical AND

slide-34
SLIDE 34

Conditional execution

Logic rules in R

TRUE & TRUE # & is the logical AND [1] TRUE

slide-35
SLIDE 35

Conditional execution

Logic rules in R

TRUE & TRUE # & is the logical AND [1] TRUE TRUE & FALSE

slide-36
SLIDE 36

Conditional execution

Logic rules in R

TRUE & TRUE # & is the logical AND [1] TRUE TRUE & FALSE [1] FALSE

slide-37
SLIDE 37

Conditional execution

Logic rules in R

TRUE & TRUE # & is the logical AND [1] TRUE TRUE & FALSE [1] FALSE TRUE | FALSE # | is the logical OR

slide-38
SLIDE 38

Conditional execution

Logic rules in R

TRUE & TRUE # & is the logical AND [1] TRUE TRUE & FALSE [1] FALSE TRUE | FALSE # | is the logical OR [1] TRUE

slide-39
SLIDE 39

Conditional execution

Logic rules in R

TRUE & TRUE # & is the logical AND [1] TRUE TRUE & FALSE [1] FALSE TRUE | FALSE # | is the logical OR [1] TRUE 5 > 3 & 0 != 1

slide-40
SLIDE 40

Conditional execution

Logic rules in R

TRUE & TRUE # & is the logical AND [1] TRUE TRUE & FALSE [1] FALSE TRUE | FALSE # | is the logical OR [1] TRUE 5 > 3 & 0 != 1 5 > 3 & 0 != 0

slide-41
SLIDE 41

Conditional execution

Logic rules in R

TRUE & TRUE # & is the logical AND [1] TRUE TRUE & FALSE [1] FALSE TRUE | FALSE # | is the logical OR [1] TRUE 5 > 3 & 0 != 1 5 > 3 & 0 != 0 as.integer(TRUE); as.integer(FALSE) [1] 1 # the internal representation of TRUE is 1 [1] 0 # the internal representation of FALSE is 0

slide-42
SLIDE 42

Conditional execution

Conditional execution

if(), else() and ifelse()

slide-43
SLIDE 43

Conditional execution

Conditional execution

if(), else() and ifelse() Syntax: if ( condition ) { commands1 } if ( condition ) { commands1 } else { commands2 } ifelse ( conditions vector, yes vector, no vector )

slide-44
SLIDE 44

Conditional execution

Conditional execution

if(), else() and ifelse() Syntax: if ( condition ) { commands1 } if ( condition ) { commands1 } else { commands2 } ifelse ( conditions vector, yes vector, no vector ) Example: x <- 4 if (x==5) {x <- x+1} else {x <- x*2}

slide-45
SLIDE 45

Conditional execution

Conditional execution

if(), else() and ifelse() Syntax: if ( condition ) { commands1 } if ( condition ) { commands1 } else { commands2 } ifelse ( conditions vector, yes vector, no vector ) Example: x <- 4 if (x==5) {x <- x+1} else {x <- x*2} x [1] 8

slide-46
SLIDE 46

Conditional execution

Organization of the script and indentation

x <- 8 if ( x != 5 & x>3 ) { x <- x+1 17+2 } else { x <- x*2 21+5 }

slide-47
SLIDE 47

Conditional execution

Organization of the script and indentation

x <- 8 if ( x != 5 & x>3 ) { x <- x+1 17+2 } else { x <- x*2 21+5 } [1] 19 x [1] 9

slide-48
SLIDE 48

Conditional execution

Another example

T <- TRUE F <- FALSE if ( T & F ) { print("T & F is TRUE") } else { print("T & F is FALSE") }

slide-49
SLIDE 49

Conditional execution

Another example

T <- TRUE F <- FALSE if ( T & F ) { print("T & F is TRUE") } else { print("T & F is FALSE") } [1] T & F is FALSE

slide-50
SLIDE 50

Conditional execution

Another example

T <- TRUE F <- FALSE if ( T & F ) { print("T & F is TRUE") } else { print("T & F is FALSE") } [1] T & F is FALSE T <- TRUE F <- FALSE if ( T | F ) { print("T | F is TRUE") } else { print("T | F is FALSE") }

slide-51
SLIDE 51

Conditional execution

Another example

T <- TRUE F <- FALSE if ( T & F ) { print("T & F is TRUE") } else { print("T & F is FALSE") } [1] T & F is FALSE T <- TRUE F <- FALSE if ( T | F ) { print("T | F is TRUE") } else { print("T | F is FALSE") } [1] T | F is TRUE

slide-52
SLIDE 52

Conditional execution

Example from Day 1

Begin Write "Enter water temperature:" Read Temp If Temp ≤ 0 then Write "This is ice" Else then If Temp < 100 then Write "This is liquid" Else then Write "This is vapor" End of If End of If End

slide-53
SLIDE 53

Conditional execution

Example from Day 1

Begin Write "Enter water temperature:" Read Temp If Temp ≤ 0 then Write "This is ice" Else then If Temp < 100 then Write "This is liquid" Else then Write "This is vapor" End of If End of If End Temp <- readline(prompt="Enter water + temperature: ") if (Temp <= 0) { print("This is ice") } else { if (Temp < 100) { print("This is liquid") } else { print("This is vapor") } }

Mind the sign for <=

slide-54
SLIDE 54

Conditional execution

ifelse()

y <- 1:10 z <- ifelse( y<6, y^2, y-1 )

slide-55
SLIDE 55

Conditional execution

ifelse()

y <- 1:10 z <- ifelse( y<6, y^2, y-1 ) z [1] 1 4 9 16 25 5 6 7 8 9

slide-56
SLIDE 56

Loops

Contents

1

Back to input files

2

Conditional execution

3

Loops

4

Executing a command from a script

slide-57
SLIDE 57

Loops

Loops

for(), while() and repeat()

slide-58
SLIDE 58

Loops

Loops

for(), while() and repeat() Syntax: for ( var in set ) { commands } while ( condition ) { commands } repeat { commands }

slide-59
SLIDE 59

Loops

Loops

for(), while() and repeat() Syntax: for ( var in set ) { commands } while ( condition ) { commands } repeat { commands } break stops all loops next goes directly to the next iteration of the loop

slide-60
SLIDE 60

Loops

Examples

x <- 0 for ( i in 1:5 ) { if (i==3) { next } ; x <- x + i }

slide-61
SLIDE 61

Loops

Examples

x <- 0 for ( i in 1:5 ) { if (i==3) { next } ; x <- x + i } # i=3 is skipped, so x <- 1+2+4+5 x [1] 12

slide-62
SLIDE 62

Loops

Examples

x <- 0 for ( i in 1:5 ) { if (i==3) { next } ; x <- x + i } # i=3 is skipped, so x <- 1+2+4+5 x [1] 12 y <- 1; j <- 1 while ( y < 12 & j < 8 ) { y <- y*2 ; j <- j + 1}

slide-63
SLIDE 63

Loops

Examples

x <- 0 for ( i in 1:5 ) { if (i==3) { next } ; x <- x + i } # i=3 is skipped, so x <- 1+2+4+5 x [1] 12 y <- 1; j <- 1 while ( y < 12 & j < 8 ) { y <- y*2 ; j <- j + 1} y ; j [1] 16 [1] 5

slide-64
SLIDE 64

Loops

Examples

x <- 0 for ( i in 1:5 ) { if (i==3) { next } ; x <- x + i } # i=3 is skipped, so x <- 1+2+4+5 x [1] 12 y <- 1; j <- 1 while ( y < 12 & j < 8 ) { y <- y*2 ; j <- j + 1} y ; j [1] 16 [1] 5 z <- 3 repeat { z<- z^2; if ( z>100 ) { break }; print(z)}

slide-65
SLIDE 65

Loops

Examples

x <- 0 for ( i in 1:5 ) { if (i==3) { next } ; x <- x + i } # i=3 is skipped, so x <- 1+2+4+5 x [1] 12 y <- 1; j <- 1 while ( y < 12 & j < 8 ) { y <- y*2 ; j <- j + 1} y ; j [1] 16 [1] 5 z <- 3 repeat { z<- z^2; if ( z>100 ) { break }; print(z)} [1] 9 [1] 81 The loop stopped after 81ˆ2 so z is 6561.

slide-66
SLIDE 66

Executing a command from a script

Contents

1

Back to input files

2

Conditional execution

3

Loops

4

Executing a command from a script

slide-67
SLIDE 67

Executing a command from a script

Executing a command from a script

R scripts and stored in .R or .r files and are executed with the command source()

slide-68
SLIDE 68

Executing a command from a script

Executing a command from a script

R scripts and stored in .R or .r files and are executed with the command source() source(’C:/Documents/R/myscript.R’)

slide-69
SLIDE 69

Executing a command from a script

Executing a command from a script

R scripts and stored in .R or .r files and are executed with the command source() source(’C:/Documents/R/myscript.R’) You can specify the current working directory using the command setwd()

slide-70
SLIDE 70

Executing a command from a script

Executing a command from a script

R scripts and stored in .R or .r files and are executed with the command source() source(’C:/Documents/R/myscript.R’) You can specify the current working directory using the command setwd() setwd(’C:/Documents/R’) getwd()

slide-71
SLIDE 71

Executing a command from a script

Executing a command from a script

R scripts and stored in .R or .r files and are executed with the command source() source(’C:/Documents/R/myscript.R’) You can specify the current working directory using the command setwd() setwd(’C:/Documents/R’) getwd() From a command line terminal, you can execute your script directly without opening an R session with Rscript myscript.R

slide-72
SLIDE 72

Executing a command from a script

Executing a command from a script

R scripts and stored in .R or .r files and are executed with the command source() source(’C:/Documents/R/myscript.R’) You can specify the current working directory using the command setwd() setwd(’C:/Documents/R’) getwd() From a command line terminal, you can execute your script directly without opening an R session with Rscript myscript.R

slide-73
SLIDE 73

Executing a command from a script

Tomorrow

To be continued ...