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