basics of algorithmics in r
play

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


  1. 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 plotted? Answer: A histogramm and a scatterplot Dr. Noémie Becker Dr. Eliza Argyridou How will the two plots be positioned? Answer: on top of each other Special thanks to : Dr. Sonja Grath for addition to slides What you should know after days 7 & 8 Comparisons in R Examples: Review: Data frames and import your data # A r e b o t h s i d e s ( n o t ) e q u a l ? Conditional execution in R 4 = = 4 [ 1 ] T R U E ● Logic rules 4 = = 5 ● if(), else(), ifelse() [ 1 ] F A L S E ● Example from day 1 2 ! = 3 [ 1 ] T R U E Loops 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 ? Executing a command from a script 3 < = 5 Writing your own functions 5 > = 2 * 2 What would you expect? How to avoid slow R code c o s ( p i / 2 ) = = 0 Caution: Never compare 2 numerical values with == cos(pi/2) == 0 [1] FALSE c o s ( p i / 2 ) cos(pi/2) [1] 6.123234e-17 # R does not answer with 0 → All these examples evaluate to TRUE or FALSE 3 4 Boolean operators Conditional execution in R ● if Logical AND (&) ● if … else FALSE & FALSE: FALSE ● ifelse FALSE & TRUE: FALSE Syntax: TRUE & FALSE: FALSE Try yourself (in R): YOUR TURN if (condition) {commands} TRUE & TRUE: TRUE T R U E & T R U E if (condition) {commands1} else {commands2} T R U E & F A L S E ifelse (condition, commands for 'TRUE', commands for 'FALSE') Logical OR (|) T R U E | F A L S E 5 > 3 & 0 ! = 1 FALSE | FALSE: FALSE Examples: 5 > 3 & 0 ! = 0 FALSE | TRUE: TRUE 5 > 3 | 0 ! = 1 a < - 1 ; b < - 2 TRUE | FALSE: TRUE i f ( a < b ) { " a i s s m a l l e r t h a n b " } TRUE | TRUE: TRUE # I n t e r n a l r e p r e s e n t a t i o n a < - 1 0 ; b < - 2 # o f T R U E a n d F A L S E Logical NOT (!) i f ( a < b ) { " a i s s m a l l e r t h a n b " } e l s e { a s . i n t e g e r ( T R U E ) " a i s n o t s m a l l e r t h a n b " !FALSE: TRUE a s . i n t e g e r ( F A L S E ) } !TRUE: FALSE 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 o t s m a l l e r t h a n b " ) 5 6

  2. Conditional execution in R A more complex example Examples (continued): x < - 8 i f ( x ! = 5 & x > 3 ) { x < - x + 1 y < - 1 : 1 0 1 7 + 2 Y } e l s e { [ 1 ] 1 2 3 4 5 6 7 8 9 1 0 x < - x * 2 z < - i f e l s e ( y < 6 , y ^ 2 , y - 1 ) 2 1 + 5 z } [ 1 ] 1 4 9 1 6 2 5 5 6 7 8 9 Mind the indentation and structure of the code if else Test → TRUE Test → FALSE What is the result of the program? (or: what does the program return after the execution of the code?) YOUR TURN i f e l s e ( y < 6 , y ^ 2 , y - 1 ) What is the value of 'x' after the execution of the code? [ 1 ] 1 9 Test → TRUE|FALSE > x [ 1 ] 9 7 8 Example from Day 1 What you should know after days 7 & 8 Pseudo-Code 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 T e m p < - r e a d l i n e ( p r o 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 < = 0 ) { How to avoid slow R code 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 0 0 ) { 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 o r . " ) } 9 10 } Loops Loops in R We will consider three types of loops for(), while() and repeat() • f Syntax: o r ( ) f o r ( v a r i n s e t ) { c o m m a n d s } • w h i l e ( ) or with better indentation (important when loops embedded) f o r ( v a r i n s e t ) { • r e p e a t ( ) c o m m a n d s } Syntax f o r ( v a r i n s e t ) { c o m m a n d s } w h i l e ( c o n d i t i o n ) { c o m m a n d s } w h i l e ( c o n d i t i o n ) { c o m m a n d s } r e p e a t { c o m m a n d s } r e p e a t { c o m m a n d s } stops all loops b r e a k goes directly into the next iteration of the loop n e x t 11 12

  3. for loop while loop Example from Day 1: f o r ( i i n 1 : 5 ) { p r i n t ( i ) } x < - 1 0 w h i l e ( x > = 0 ) { x < - x – 1 } What will R do? YOUR TURN # x a f t e r t h e l o o p : Print numbers 1 to 5 YOUR TURN x [ 1 ] - 1 x < - 0 f o r ( i i n 1 : 5 ) { i f ( i = = 3 ) { n e x t } ; x < - x + i } # v a l u e s o f x a f t e r e a c h i t e r a t i o n : YOUR TURN x < - 1 0 # x a f t e r t h e l o o p : w h i l e ( x > = 0 ) { x < - x – 1 ; p r i n t ( x ) } x YOUR TURN [ 1 ] 9 [ 1 ] 1 2 [ 1 ] 8 [ 1 ] 7 # i = 3 i s s k i p p e d , s o x < - 1 + 2 + 4 + 5 [ 1 ] 6 # v a l u e s o f x a f t e r e a c h i t e r a t i o n : [ 1 ] 5 x < - 0 [ 1 ] 4 f o r ( i i n 1 : 5 ) { i f ( i = = 3 ) { n e x t } ; x < - x + i ; p r i n t ( x ) } [ 1 ] 3 [ 1 ] 1 [ 1 ] 2 [ 1 ] 3 [ 1 ] 1 [ 1 ] 7 [ 1 ] 0 13 14 [ 1 ] 1 2 [ 1 ] - 1 while loop repeat y < - 1 ; j < - 1 Syntax: w h i l e ( y < 1 2 & j < 8 ) { y < - y * 2 ; j < - j + 1 } r e p e a t { s t a t e m e n t } # y a n d j a f t e r t h e l o o p : y ; j In the statement block, we must use the b statement to exit the loop. YOUR TURN [ 1 ] 1 6 r e a k [ 1 ] 5 z < - 3 # x a n d j a f t e r e a c h i t e r a t i o n : r e p e a t { z < - z ^ 2 ; i f ( z > 1 0 0 ) { b r e a k } ; p r i n t ( z ) } y < - 1 ; j < - 1 # W h a t i s z ? YOUR TURN w h i l e ( y < 1 2 & j < 8 ) { z YOUR TURN y < - y * 2 ; j < - j + 1 ; [ 1 ] 6 5 6 1 p r i n t ( p a s t e ( " y = " , y , " j = " , j , s e p = " " ) ) } The loop stopped after 81^2. Therefore, z is 6561 [ 1 ] " y = 2 j = 2 " [ 1 ] " y = 4 j = 3 " [ 1 ] " y = 8 j = 4 " [ 1 ] " y = 1 6 j = 5 " 15 16 What you should know after days 7 & 8 Executing a command from a script Review: Data frames and import your data R scripts are stored in .R or .r files and can be executed in a script Conditional execution in R with the command source() : ● Logic rules s o u r c e ( " / D o c u m e n t s / R / m y s c r i p t . R " ) ● if(), else(), ifelse() You can get the current w orking d irectory with getwd() : ● Example from day 1 g e t w d ( ) Loops # t r y : Executing a command from a script m y P a t h < - g e t w d ( ) You can set the current w orking d irectory with setwd() : Writing your own functions s e t w d ( " / D o c u m e n t s / R " ) How to avoid slow R code # 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 17 18

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

Recommend


More recommend