Relational operators Lore Dirick Instructor, DataCamp Intermediate - - PowerPoint PPT Presentation

relational operators
SMART_READER_LITE
LIVE PREVIEW

Relational operators Lore Dirick Instructor, DataCamp Intermediate - - PowerPoint PPT Presentation

INTERMEDIATE R FOR FINANCE Relational operators Lore Dirick Instructor, DataCamp Intermediate R for Finance A relational example > today <- 54.33 > yesterday <- 55.24 < > today < yesterday [1] TRUE Intermediate R for


slide-1
SLIDE 1

INTERMEDIATE R FOR FINANCE

Relational operators

Lore Dirick

Instructor, DataCamp

slide-2
SLIDE 2

Intermediate R for Finance

A relational example

> today <- 54.33 > yesterday <- 55.24 > today < yesterday [1] TRUE

<

slide-3
SLIDE 3

Intermediate R for Finance

Relational operators

Format Description > Greater than >= Greater than or equal to < Less than <= Less than or equal to == Equal != Not equal

slide-4
SLIDE 4

Intermediate R for Finance

Relational operator examples

> one <- 1 > one == TRUE [1] TRUE > apple <- c(120.00, 120.08, 119.97, 121.88) > apple < 121 [1] TRUE TRUE TRUE FALSE

slide-5
SLIDE 5

INTERMEDIATE R FOR FINANCE

Let’s practice!

slide-6
SLIDE 6

INTERMEDIATE R FOR FINANCE

Logical operators

slide-7
SLIDE 7

Intermediate R for Finance

Logical operators

Operator Description Math Representation & And Intersection | Or Union ! Not Negate

slide-8
SLIDE 8

Intermediate R for Finance

Logical operator examples

> datacamp <- 64.69 > # And > (datacamp > 64) & (datacamp < 65) [1] TRUE > apple <- 124 > # Or > (datacamp > 63) | (apple > 126) [1] TRUE

slide-9
SLIDE 9

Intermediate R for Finance

Logical operators + subset()

> cash <- data.frame( company = c("A", "A", "A", "B", "B", "B", "B"), cash_flow = c(1000, 4000, 550, 1500, 1100, 750, 6000), year = c(1, 3, 4, 1, 2, 4, 5)) > # Filter for rows with low cash flow > subset(cash, cash_flow < 1000) company cash_flow year 3 A 550 4 6 B 750 4 > # Filter for company A's low cash flow > subset(cash, cash_flow < 1000 & company == "A") company cash_flow year 3 A 550 4

slide-10
SLIDE 10

INTERMEDIATE R FOR FINANCE

Let’s practice!

slide-11
SLIDE 11

INTERMEDIATE R FOR FINANCE

If statements

slide-12
SLIDE 12

Intermediate R for Finance

If statements

Test Condition If True Body False > if(condition) { code }

slide-13
SLIDE 13

Intermediate R for Finance

Default if…

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

slide-14
SLIDE 14

Intermediate R for Finance

If-else

Test Condition True Body - if False > if(condition) { code if true } else { code if false } Body - else If

slide-15
SLIDE 15

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!") } [1] "No problems here!"

slide-16
SLIDE 16

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 }

slide-17
SLIDE 17

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!") } [1] "Warning! Starting to get risky."

slide-18
SLIDE 18

INTERMEDIATE R FOR FINANCE

Let’s practice!