If statements

Intermediate R for Finance

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

If statements

ch2_vid3_slides.003.png

if(condition) {
   code
}
Intermediate R for Finance

Default if…

default_chance <- runif(1)

if(default_chance > .5) { print("Take action! Likely to default!") }
"Take action! Likely to default!"
default_chance
0.9484406
default_chance <- runif(1)

if(default_chance > .5) { print("Take action! Likely to default!") } default_chance
0.3954069
Intermediate R for Finance

If-else

ch2_vid3_slides.015.png

if(condition) {
  code if true
} else {
    code if false 
}
Intermediate R for Finance

Default if…else…

default_chance <- .4
if(default_chance > .5) {
  print("Take action! Likely to default!")
} else {
  print("No problems here!")
}
"No problems here!"
Intermediate R for Finance

If-else if-else

if(condition1) {
  code if condition1 is true
} else if(condition2) {
    code if condition2 is true
} else {
    code if both are false
Intermediate R for Finance

Default if...else if...else

default_chance <- .4

if(default_chance > .5) {
  print("Take action! Likely to default!")
} else if(default_chance > .3 & default_chance <= .5) {
  print("Warning! Starting to get risky.")
} else {
  print("No problems here!")
}
"Warning! Starting to get risky."
Intermediate R for Finance

Let's practice!

Intermediate R for Finance

Preparing Video For Download...