Relational Operators for Numeric Vectors

x = c(1.0, 1.2, 1.4, 1.6, 1.8)
x == 1.2
#> [1] FALSE  TRUE FALSE FALSE FALSE
x != 1.2
#> [1]  TRUE FALSE  TRUE  TRUE  TRUE
x >  1.2
#> [1] FALSE FALSE  TRUE  TRUE  TRUE
x >= 1.2
#> [1] FALSE  TRUE  TRUE  TRUE  TRUE
x <  1.2
#> [1]  TRUE FALSE FALSE FALSE FALSE
x <= 1.2
#> [1]  TRUE  TRUE FALSE FALSE FALSE

Relational Operators for Character Vectors (and Factors)

x = c("aa", "bb", "aa", "bb", "aa", "cc", "dd")
x == "aa"
#> [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE
x != "aa"
#> [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE
x %in% c("aa","bb")
#> [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
!(x %in% c("aa","bb"))  # x not in set
#> [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE

Note: the above works for factors. But R also supports ordered factors that will recognize numeric-like relational operators

y = factor(x, ordered=TRUE)
y
#> [1] aa bb aa bb aa cc dd
#> Levels: aa < bb < cc < dd
y <= "bb"
#> [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE

Logical Operators

x = c(TRUE,FALSE,TRUE,FALSE)
y = c(TRUE,FALSE,FALSE,TRUE)
!x
#> [1] FALSE  TRUE FALSE  TRUE
x & y
#> [1]  TRUE FALSE FALSE FALSE
x && y
#> [1] TRUE
x | y
#> [1]  TRUE FALSE  TRUE  TRUE
x || y
#> [1] TRUE
xor(x,y)
#> [1] FALSE FALSE  TRUE  TRUE
isTRUE(x==y)
#> [1] FALSE
which(x)
#> [1] 1 3
which(1:5>3)
#> [1] 4 5
all(x)
#> [1] FALSE
any(x)
#> [1] TRUE

Set Operators

x = c(1,2,3,4,5,5)
y = c(2,4,6)
union(x,y)
#> [1] 1 2 3 4 5 6
intersect(x,y)
#> [1] 2 4
setdiff(x,y)
#> [1] 1 3 5
setdiff(y,x)
#> [1] 6
setequal(x,y)
#> [1] FALSE
is.element(x,y)
#> [1] FALSE  TRUE FALSE  TRUE FALSE FALSE
x %in% y
#> [1] FALSE  TRUE FALSE  TRUE FALSE FALSE
match(x,y)
#> [1] NA  1 NA  2 NA NA
match(y,x)
#> [1]  2  4 NA
match(4,y)
#> [1] 2

Control Flow

x = c(1,2,3,4,5,5)
y = c(2,4,6)
if(any(x == 5)) print("There is a 5!")
#> [1] "There is a 5!"
if(all(x == 5)) print("All 5's ") else print("Not all 5's")
#> [1] "Not all 5's"
ifelse(x==5,x^2,x)      # Square all fives. 
#> [1]  1  2  3  4 25 25
if(TRUE & TRUE & NA) print("Hello World")     # if doesn't work with NA's
#> Error in if (TRUE & TRUE & NA) print("Hello World"): missing value where TRUE/FALSE needed
if(TRUE && FALSE && NA) print("Hello World")  # && stops evaluating at first FALSE so NA does not get evaluated
z = 0; for(i in 1:10) z[i] = 2*i; z
#>  [1]  2  4  6  8 10 12 14 16 18 20
w = 0; i = 1; while(i<=10){ w[i] = 2*i; i = i+1}; w
#>  [1]  2  4  6  8 10 12 14 16 18 20

Testing For Equality

x = 1:10                   # integer vector of length 10
y = matrix(x,nrow=1)       # 1 x 10 matrix
identical(x,y)             # they are not identical
#> [1] FALSE
all.equal(x,y)             # because they are not same type (class) of object
#> [1] "Attributes: < target is NULL, current is list >"
#> [2] "target is numeric, current is matrix"
identical(x,as.integer(y)) # now they are identical
#> [1] TRUE
(sqrt(2) ^ 2) == 2
#> [1] FALSE
dplyr::near((sqrt(2) ^ 2), 2)
#> [1] TRUE

Index Filtering

#- subset with logicals or indices
big = (iris$Sepal.Length > 7.5)        # logical vector
ind = which(iris$Sepal.Length > 7.5)   # integer vector of indices
identical(iris[big,],iris[ind,])
#> [1] TRUE

#- Find element which is minimum or maximum
which.max(iris$Sepal.Length)
#> [1] 132
which(iris$Sepal.Length == max(iris$Sepal.Length))
#> [1] 132

#- match()
a = -5:5
b = 3:5
match(b,a)
#> [1]  9 10 11

#- match and combine data (see merge() for a better approach)
x = data.frame(uppercase=LETTERS[1:10],lowercase=letters[1:10],position=NA)
y = data.frame(uppercase=c("B","C","A","E","D"), position=c(2,3,1,5,4)) # out of order and partial
ind.x = match(y$uppercase,x$uppercase)  # index of x
ind.x
#> [1] 2 3 1 5 4
x$position[ind.x] = y$position          # add position info to x
x$uppercase %in% y$uppercase
#>  [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE

#- missing
a[3] = NA   # set the third element to missing (NA)
is.na(a)
#>  [1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE