Logical operators

Intermediate R for Finance

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

Logical operators

Operator Description Math representation
& AND Conjunction (∧)
| OR Disjunction (∨)
! NOT Negation (¬)
Intermediate R for Finance

Logical operators

Operator Description Math representation
& AND Conjunction (∧)
| OR Disjunction (∨)
! NOT Negation (¬)
Intermediate R for Finance

Logical operators

Operator Description Math representation
& AND Conjunction (∧)
| OR Disjunction (∨)
! NOT Negation (¬)
Intermediate R for Finance

Logical operators

Operator Description Math representation
& AND Conjunction (∧)
| OR Disjunction (∨)
! NOT Negation (¬)
Intermediate R for Finance

Logical operators

Operator Description Math representation
& AND Conjunction (∧)
| OR Disjunction (∨)
! NOT Negation (¬)
Intermediate R for Finance

Logical operator examples

datacamp <- 64.69

# And
(datacamp > 64) & (datacamp < 65)
TRUE
apple <- 124

# Or
(datacamp > 63) | (apple > 126)
TRUE
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
Intermediate R for Finance

Let's practice!

Intermediate R for Finance

Preparing Video For Download...