Sep 05, 2017

Relational Operators (1)

> TRUE == TRUE
[1] TRUE
> TRUE == FALSE
[1] FALSE
> TRUE != TRUE
[1] FALSE
> TRUE != FALSE
[1] TRUE

Relational Operators (2)

> "hello" == "hola"
[1] FALSE
> 5 == 2
[1] FALSE
> "hello" != "hola"
[1] TRUE
> 5 != 2
[1] TRUE

Relational Operators (3)

> 2 < 5
[1] TRUE
> 2 > 5
[1] FALSE
> "adios" > "goodbye"  # alphabetical order!
[1] FALSE
> FALSE < TRUE         # FALSE coerces to 0 and TRUE coerces to 1
[1] TRUE

Relational Operators (4)

> 5 >= 2
[1] TRUE
> 5 >= 5
[1] TRUE
> 5 <= 5
[1] TRUE

Relational Operators & Vectors

> boone_temp <- c(81, 72, 73, 75, 69)
> boone_temp
[1] 81 72 73 75 69
> boone_temp > 72
[1]  TRUE FALSE  TRUE  TRUE FALSE
> boone_temp >= 72
[1]  TRUE  TRUE  TRUE  TRUE FALSE

Logical Operators

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

AND operator (&)

> TRUE & TRUE       # Only TRUE if both are TRUE
[1] TRUE
> FALSE & TRUE      # FALSE if both not TRUE
[1] FALSE
> TRUE & FALSE
[1] FALSE
> FALSE & FALSE
[1] FALSE

AND operator (&)

> x <- 15
> x > 5 & x < 20    # BOTH conditions are TRUE
[1] TRUE
> x > 5 & x < 14    # ONLY one condition is TRUE 
[1] FALSE

OR operator (1)

> TRUE | TRUE   # TRUE if at least one is TRUE
[1] TRUE
> TRUE | FALSE  # ONLY FALSE if both are FALSE
[1] TRUE
> FALSE | TRUE
[1] TRUE
> FALSE | FALSE
[1] FALSE

OR operator (2)

> y <- 11
> y < 15 | y > 15  # At least one TRUE -> TRUE
[1] TRUE
> y < 10 | y > 12  # FALSE if both FALSE
[1] FALSE

NOT operator (!)

> !TRUE
[1] FALSE
> !FALSE
[1] TRUE
> x
[1] 15
> !(x <= 3)
[1] TRUE

NOT operator (!)

> is.numeric(10)
[1] TRUE
> !is.numeric(10)
[1] FALSE
> is.numeric("hola")
[1] FALSE
> !is.numeric("hola")
[1] TRUE

Logical Operators & Vectors

> c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)
[1]  TRUE FALSE FALSE
> c(TRUE, TRUE, FALSE) | c(TRUE, FALSE, FALSE)
[1]  TRUE  TRUE FALSE
> !c(TRUE, TRUE, FALSE)
[1] FALSE FALSE  TRUE

TEASER - Explain what is happening

> c(TRUE, TRUE, FALSE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)
Warning in c(TRUE, TRUE, FALSE, TRUE, FALSE) & c(TRUE, FALSE, FALSE):
longer object length is not a multiple of shorter object length
[1]  TRUE FALSE FALSE  TRUE FALSE

Vector and (Scalar) Operators

> c(TRUE, TRUE, FALSE) & c(FALSE, FALSE, FALSE)
[1] FALSE FALSE FALSE
> c(TRUE, TRUE, FALSE) && c(FALSE, FALSE, FALSE)
[1] FALSE
> c(TRUE, TRUE, FALSE) | c(FALSE, FALSE, FALSE)
[1]  TRUE  TRUE FALSE
> c(TRUE, TRUE, FALSE) || c(FALSE, FALSE, FALSE)
[1] TRUE

if statement (1)

> if(condition){
+   expression
+ }
> x <- 5
> if(x >= 0){
+   print("x is a positive number")
+ }
[1] "x is a positive number"

if statement (2)

> if(condition){
+   expression
+ }
> x <- 5
> if(x < 0){                        # condition is FALSE
+   print("x is a negative number") # Print not executed 
+ }                                 # No output!

else statement (1)

> if(condition){
+   expression1
+ } else {
+   expression2
+ }

else statement (2)

> if(condition){
+   expression1
+ } else {
+   expression2
+ }
> x <- -5
> if(x < 0){   #Condition is TRUE
+   print("x is a negative number")
+ } else {
+   print("x is either a positive number or zero")  
+ }
[1] "x is a negative number"

else statement (3)

> if(condition){
+   expression1
+ } else {
+   expression2
+ }
> x <- 5
> if(x < 0){   #Condition is TRUE
+   print("x is a negative number")
+ } else {
+   print("x is either a positive number or zero")  
+ }
[1] "x is either a positive number or zero"

else if statement

> x <- -3
> if(x < 0) {
+   print("x is a negative number")
+ } else if(x == 0) {
+   print("x is zero")
+ } else {
+   print("x is a positive number")
+ }
[1] "x is a negative number"
> # else if TEMPLATE
> if(condition1) {
+   expression1
+ } else if(condition2) {
+   expression2
+ } else {
+   expression3
+ }

else if statement (2)

> x <- 0
> if(x < 0) {
+   print("x is a negative number")
+ } else if(x == 0) {
+   print("x is zero")
+ } else {
+   print("x is a positive number")
+ }
[1] "x is zero"
> # else if TEMPLATE
> if(condition1) {
+   expression1
+ } else if(condition2) {
+   expression2
+ } else {
+   expression3
+ }

else statement

> x <- 5
> if(x < 0) {         #Cond is FALSE
+   print("x is a negative number")
+ } else if(x == 0) { #Cond is FALSE
+   print("x is zero")
+ } else {
+   print("x is a positive number")
+ }
[1] "x is a positive number"
> # TEMPLATE
> if(condition1) {
+   expression1
+ } else if(condition2) {
+   expression2
+ } else {
+   expression3
+ }

if, else if, else

> x <- 9
> if(x %% 2 == 0) {        
+   print("divisible by 2")
+ } else if(x %% 3 == 0) { 
+   print("divisible by 3")
+ } else {
+   print("not divisible by 2 nor by 3...")
+ }
[1] "divisible by 3"
> # TEMPLATE
> if(condition1) {
+   expression1
+ } else if(condition2) {
+   expression2
+ } else {
+   expression3
+ }