==
equal to!=
not equal to>
greater than>=
greater than or equal to<
less than<=
less than or equal tox = 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
==
equal to!=
not equal to%in%
element of set (use: x %in% set
)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
!
negation (NOT)&
elementwise AND (returns a vector)&&
first element AND (most useful in if
clauses)|
elementwise OR (returns a vector)||
first element OR (most useful in if
clauses)xor
elementwise exclusive ORisTRUE(x)
returns TRUE only if argument x = TRUE
(single value)which(x)
returns the indices that are TRUE
all(x)
are all values TRUE
?any(x)
are some values TRUE
?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
union(x,y)
in either x or y (or both)intersect(x,y)
in both x and ysetdiff(x,y)
in x, but not ysetequal(x,y)
returns TRUE
if same elements in each setis.element(element,set)
returns logical vector if element(s) are in set%in%
shortcut for is.element() (use: element %in% set
)match(x,table)
finds index of 1st match (NA
if x not in table)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
if(test)
if test==TRUE
, then execute following expression(s). Use: if(test) expr
or if(test) cons.expr else alt.expr
ifelse(tests,yes,no)
vectorized loop: for the TRUE
elements of tests
, return yes
, no
for FALSE
elements.for
loop over elements in seq
. Use: for(var in seq) expr
while
loop under condition is FALSE
. Use: while(cond) expr
repeat
loop continues until a break
break
breaks out of a loopnext
stops current flow and advances to next looping indexswitch(expr,...)
switch between expressions depending on value of expr
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
all.equal(x,y)
tests if objects x and y are (nearly) equalidentical(x,y)
tests if objects x and y are (exactly) equaldplyr::near(x,y,tol)
tests if numeric objects x and y are within tolerancex = 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
which(x)
returns the indices that are TRUE
which.min()
The index of the minimum value (first index if ties)which.max()
The index of the maximum value (first index if ties)match(x,table)
find position of x values in table%in%
element of set (use: x %in% set
)is.na()
returns the indices that have missing (NA
) values.#- 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