Relational Operators

Eqaulity ==

Logical values
TRUE == TRUE
TRUE

TRUE == FALSE
FALSE

#InEquality !=
"hello" ! = "goodbye"
TRUE

TRUE ! = FALSE
TRUE

# < and >
3<5
TRUE bye

3>5
FALSE

#Alphabetical Order !
"Hello" > "Goodbye"
TRUE

#TRUE correspond to 1, FALSE corresponds to 0
TRUE < FALSE
FALSE

#<= and >=

Equality

Basic form of comparision: equality

3 == (2 + 1)
"intermediate" != "r"
TRUE ! = FALSE
"Rchitect" != "rechitect"

Compare vectors

Using relational operators, find a logical answer, i.e. TRUE or FALSE, for the following questions:

On which days did the number of LinkedIn profile views exceed 15?
When was your LinkedIn profile viewed only 5 times or fewer?
When was your LinkedIn profile visited more often than your Facebook profile?

# The linkedin and facebook vectors have already been created for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)

# Popular days
linkedin > 15

# Quiet days
linkedin <= 5

# LinkedIn more popular than Facebook
linkedin > facebook

Logical Operators

TREU & TRUE
TRUE

FALSE & TRUE
FALSE

AND Operator "&
x <- 12
x > 5 & x < 15
TRUE

x <- 17
x > 5 & x < 15
FALSE

OR operation |
TRUE | TRUE
TRUE

TRUE | FALSE
TRUE

FALSE | FALSE
FALSE

is not exclusive

y <- 4
y < 5 | y > 15
TRUE

y <- 14
y < 5 | y > 15
FALSE

NOT operator "!"
!TRUE
FALSE

!FALSE
TRUE

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

Logical Operator


**And operator &** 
Half false is false

TRUE & TRUE
TRUE

FALSE & TRUE
FALSE

X<- 12
x > 4 & x <15
TRUE

x <- 17
x > 5 & x < 15
FALSE

**Or operator |**
Half True is TRUE
TRUE | TRUE
TRUE

FALSE | TRUE
TRUE

TRUE | FALSE
TRUE

FALSE | FALSE
FALSE

**Not operator !**
!TRUE
FALSE

!FALSE 
TRUE

is.numeric(5)
TRUE

!is.numeric(5)
FALSE

**Logical operators & Vectors**
	c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)
FALSE FALSE FALSE

c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)
TRUE TRUE FALSE

!c(TRUE, TRUE, FALSE)
FALSE FALSE TRUE

"&" vs "&&", "|" vs "||"
c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)
TRUE FALSE FALSE

c(TRUE, TRUE, FALSE) && c(TRUE, FALSE, FALSE)
TRUE

& and |

TRUE & TRUE
FALSE | TRUE
5 <= 5 & 2 < 3
3 < 4 | 7 < 6

3 < x < 7
3 < x & x < 7

linkedin <- c(16, 9, 13, 5, 2, 17, 14)
last <- tail(linkedin, 1)

#Is last under 5 or above 10
5 < last & last < 10

#Is last between 15(exclusive) and 20 (inclusive)
15 < last | last < 20

& and | (2)

# The social data (linkedin, facebook, views) has been created for you

# linkedin exceeds 10 but facebook below 10
linkedin > 10 & facebook < 10

# When were one or both visited at least 12 times?
linkedin >= 12 | facebook >= 12

# When is views between 11 (exclusive) and 14 (inclusive)?
views > 11 & views <= 14

Reverse the result: !

!TRUE 
FALSE

!(5 > 3)
FALSE

!!FALSE
FALSE

x <- 5
y <- 7
!(!(x < 4) & !!!(y > 12))

Conditional statement