Basics of Algorithmics in R h i s t ( d a t a $ v a r 1 - - PDF document

basics of algorithmics in r
SMART_READER_LITE
LIVE PREVIEW

Basics of Algorithmics in R h i s t ( d a t a $ v a r 1 - - PDF document

An introduction to WS 2019/2020 p a r ( m f r o w = c ( 2 , 1 ) ) Basics of Algorithmics in R h i s t ( d a t a $ v a r 1 ) p l o t ( d a t a $ v a r 1 , d a t a $ v a r 2 ) What will be


slide-1
SLIDE 1

p a r ( m f r

  • w

= c ( 2 , 1 ) ) h i s t ( d a t a $ v a r 1 ) p l

  • t

( d a t a $ v a r 1 , d a t a $ v a r 2 )

What will be plotted? Answer: A histogramm and a scatterplot How will the two plots be positioned? Answer: on top of each other

An introduction to WS 2019/2020

  • Dr. Noémie Becker
  • Dr. Eliza Argyridou

Special thanks to:

  • Dr. Sonja Grath for addition to slides

Basics of Algorithmics in R

3

What you should know after days 7 & 8

Review: Data frames and import your data Conditional execution in R

  • Logic rules
  • if(), else(), ifelse()
  • Example from day 1

Loops Executing a command from a script Writing your own functions How to avoid slow R code 4

Comparisons in R

Examples:

# A r e b

  • t

h s i d e s ( n

  • t

) e q u a l ? 4 = = 4 [ 1 ] T R U E 4 = = 5 [ 1 ] F A L S E 2 ! = 3 [ 1 ] T R U E 3 ! = 3 # I s t h e l e f t s i d e g r e a t e r / s m a l l e r … t h a n t h e r i g h t s i d e ? 3 < = 5 5 > = 2 * 2

What would you expect?

c

  • s

( p i / 2 ) = = c

  • s

( p i / 2 )

→ All these examples evaluate to TRUE or FALSE

Caution: Never compare 2 numerical values with == cos(pi/2) == 0 [1] FALSE cos(pi/2) [1] 6.123234e-17 # R does not answer with 0

5

Boolean operators

Logical AND (&) FALSE & FALSE: FALSE FALSE & TRUE: FALSE TRUE & FALSE: FALSE TRUE & TRUE: TRUE Logical OR (|) FALSE | FALSE: FALSE FALSE | TRUE: TRUE TRUE | FALSE: TRUE TRUE | TRUE: TRUE Logical NOT (!) !FALSE: TRUE !TRUE: FALSE

Try yourself (in R): T R U E & T R U E T R U E & F A L S E T R U E | F A L S E 5 > 3 & ! = 1 5 > 3 & ! = 5 > 3 | ! = 1 # I n t e r n a l r e p r e s e n t a t i

  • n

#

  • f

T R U E a n d F A L S E a s . i n t e g e r ( T R U E ) a s . i n t e g e r ( F A L S E ) YOUR TURN

6

Conditional execution in R

  • if
  • if … else
  • ifelse

Syntax: if (condition) {commands} if (condition) {commands1} else {commands2} ifelse (condition, commands for 'TRUE', commands for 'FALSE') Examples:

a <

  • 1

; b <

  • 2

i f ( a < b ) { " a i s s m a l l e r t h a n b " } a <

  • 1

; b <

  • 2

i f ( a < b ) { " a i s s m a l l e r t h a n b " } e l s e { " a i s n

  • t

s m a l l e r t h a n b " } i f e l s e ( a < b , " a i s s m a l l e r t h a n b " , " a i s n

  • t

s m a l l e r t h a n b " )

slide-2
SLIDE 2

7

Conditional execution in R

Examples (continued):

y <

  • 1

: 1 Y [ 1 ] 1 2 3 4 5 6 7 8 9 1 z <

  • i

f e l s e ( y < 6 , y ^ 2 , y

  • 1

) z [ 1 ] 1 4 9 1 6 2 5 5 6 7 8 9 i f e l s e ( y < 6 , y ^ 2 , y

  • 1

)

Test → TRUE|FALSE if Test → TRUE else Test → FALSE

8

A more complex example

x <

  • 8

i f ( x ! = 5 & x > 3 ) { x <

  • x

+ 1 1 7 + 2 } e l s e { x <

  • x

* 2 2 1 + 5 }

Mind the indentation and structure of the code What is the result of the program? (or: what does the program return after the execution of the code?) What is the value of 'x' after the execution of the code? [ 1 ] 1 9 > x [ 1 ] 9 YOUR TURN

9

Example from Day 1

Pseudo-Code

T e m p <

  • r

e a d l i n e ( p r

  • m

p t = " E n t e r w a t e r t e m p e r a t u r e : " ) i f ( T e m p < = ) { p r i n t ( " T h i s i s i c e . " ) } e l s e { i f ( T e m p < 1 ) { p r i n t ( " T h i s i s l i q u i d . " ) } e l s e { p r i n t ( " T h i s i s v a p

  • r

. " ) } }

10

What you should know after days 7 & 8

Review: Data frames and import your data Conditional execution in R

  • Logic rules
  • if(), else(), ifelse()
  • Example from day 1

Loops Executing a command from a script Writing your own functions How to avoid slow R code 11

Loops

We will consider three types of loops

  • f
  • r

( )

  • w

h i l e ( )

  • r

e p e a t ( ) Syntax f

  • r

( v a r i n s e t ) { c

  • m

m a n d s } w h i l e ( c

  • n

d i t i

  • n

) { c

  • m

m a n d s } r e p e a t { c

  • m

m a n d s } b r e a k stops all loops n e x t goes directly into the next iteration of the loop 12

Loops in R

for(), while() and repeat() Syntax:

f

  • r

( v a r i n s e t ) { c

  • m

m a n d s }

  • r with better indentation (important when loops embedded)

f

  • r

( v a r i n s e t ) { c

  • m

m a n d s } w h i l e ( c

  • n

d i t i

  • n

) { c

  • m

m a n d s } r e p e a t { c

  • m

m a n d s }

slide-3
SLIDE 3

13

for loop

f

  • r

( i i n 1 : 5 ) { p r i n t ( i ) } What will R do? Print numbers 1 to 5 x <

  • f
  • r

( i i n 1 : 5 ) { i f ( i = = 3 ) { n e x t } ; x <

  • x

+ i } # x a f t e r t h e l

  • p

: x [ 1 ] 1 2 # i = 3 i s s k i p p e d , s

  • x

<

  • 1

+ 2 + 4 + 5 # v a l u e s

  • f

x a f t e r e a c h i t e r a t i

  • n

: x <

  • f
  • r

( i i n 1 : 5 ) { i f ( i = = 3 ) { n e x t } ; x <

  • x

+ i ; p r i n t ( x ) } [ 1 ] 1 [ 1 ] 3 [ 1 ] 7 [ 1 ] 1 2

Example from Day 1:

YOUR TURN YOUR TURN

14

while loop

x <

  • 1

w h i l e ( x > = ) { x <

  • x

– 1 } # x a f t e r t h e l

  • p

: x [ 1 ]

  • 1

# v a l u e s

  • f

x a f t e r e a c h i t e r a t i

  • n

: x <

  • 1

w h i l e ( x > = ) { x <

  • x

– 1 ; p r i n t ( x ) } [ 1 ] 9 [ 1 ] 8 [ 1 ] 7 [ 1 ] 6 [ 1 ] 5 [ 1 ] 4 [ 1 ] 3 [ 1 ] 2 [ 1 ] 1 [ 1 ] [ 1 ]

  • 1

YOUR TURN YOUR TURN

15

while loop

y <

  • 1

; j <

  • 1

w h i l e ( y < 1 2 & j < 8 ) { y <

  • y

* 2 ; j <

  • j

+ 1 } # y a n d j a f t e r t h e l

  • p

: y ; j [ 1 ] 1 6 [ 1 ] 5 # x a n d j a f t e r e a c h i t e r a t i

  • n

: y <

  • 1

; j <

  • 1

w h i l e ( y < 1 2 & j < 8 ) { y <

  • y

* 2 ; j <

  • j

+ 1 ; p r i n t ( p a s t e ( " y = " , y , " j = " , j , s e p = " " ) ) } [ 1 ] " y = 2 j = 2 " [ 1 ] " y = 4 j = 3 " [ 1 ] " y = 8 j = 4 " [ 1 ] " y = 1 6 j = 5 "

YOUR TURN YOUR TURN

16

repeat

Syntax: r e p e a t { s t a t e m e n t } In the statement block, we must use the b r e a k statement to exit the loop. z <

  • 3

r e p e a t { z <

  • z

^ 2 ; i f ( z > 1 ) { b r e a k } ; p r i n t ( z ) } # W h a t i s z ? z [ 1 ] 6 5 6 1 The loop stopped after 81^2. Therefore, z is 6561 YOUR TURN

17

What you should know after days 7 & 8

Review: Data frames and import your data Conditional execution in R

  • Logic rules
  • if(), else(), ifelse()
  • Example from day 1

Loops Executing a command from a script Writing your own functions How to avoid slow R code 18

Executing a command from a script

R scripts are stored in .R or .r files and can be executed in a script with the command source(): s

  • u

r c e ( " / D

  • c

u m e n t s / R / m y s c r i p t . R " ) You can get the current working directory with getwd(): g e t w d ( ) # t r y : m y P a t h <

  • g

e t w d ( ) You can set the current working directory with setwd(): s e t w d ( " / D

  • c

u m e n t s / R " ) # t r y : s e t w d ( m y P a t h ) You can execute your script directly (without opening a R session) from a command line terminal with: R s c r i p t m y s c r i p t . R

slide-4
SLIDE 4

19

Take-home message

  • There are three functions used for conditional execution in R
  • if()
  • else()
  • ifelse()
  • There are three types of loop in R
  • for
  • while
  • repeat