Logical Operators

Intermediate R

Filip Schouwenaars

DataCamp Instructor

Logical Operators

  • AND operator &
  • OR operator |
  • NOT operator !
Intermediate R

AND operator "&"

TRUE & TRUE
TRUE
FALSE & TRUE
FALSE
TRUE & FALSE
FALSE
FALSE & FALSE
FALSE
Intermediate R

AND operator "&"

x <- 12
x > 5 & x < 15
TRUE
x <- 17
x > 5 & x < 15
FALSE
Intermediate R

OR operator "|"

TRUE | TRUE
TRUE
TRUE | FALSE
TRUE
FALSE | TRUE
TRUE
FALSE | FALSE
FALSE
Intermediate R

OR operator "|"

y <- 4
y < 5 | y > 15
TRUE
y <- 14
y < 5 | y > 15
FALSE
Intermediate R

NOT operator "!"

!TRUE
FALSE
!FALSE
TRUE
!(x < 5)
x >= 5
Intermediate R

NOT operator "!"

is.numeric(5)
TRUE
!is.numeric(5)
FALSE
is.numeric("hello")
FALSE
!is.numeric("hello")
TRUE
Intermediate R

Logical Operators & Vectors

c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)
TRUE FALSE FALSE
c(TRUE, TRUE, FALSE) | c(TRUE, FALSE, FALSE)
TRUE  TRUE FALSE
!c(TRUE, TRUE, FALSE)
FALSE FALSE  TRUE
Intermediate R

"&" vs "&&", "|" vs "||"

c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)
TRUE FALSE FALSE
c(TRUE, TRUE, FALSE) && c(TRUE, FALSE, FALSE)
TRUE
Intermediate R

"&" vs "&&", "|" vs "||"

c(TRUE, TRUE, FALSE) | c(TRUE, FALSE, FALSE)
TRUE  TRUE FALSE
c(TRUE, TRUE, FALSE) || c(TRUE, FALSE, FALSE)
TRUE
Intermediate R

Let's practice!

Intermediate R

Preparing Video For Download...