Conditionals are an important feature in R. This section corresponds to Datacamp’s Conditionals and Control Flow in the Intermediate R course.
==
and inequality is !=
<
and >
<=
and
>=
"a" < "b"
"a" < "A"
(lowercase
are less)TRUE
is 1
, and FALSE
is
0
TRUE > FALSE
c(1, 2) < c(2, 3)
returns
c(TRUE, TRUE)
TRUE
/FALSE
matrix."1" == 1
1 == TRUE
but "1" != TRUE
sum()
can count the number of TRUE
in a
vector/matrixYou can use the if statement to selectively execute code. You can use
a combination of if
, else if
, and
else
.
var_a <- 100
if (var_a < 100) {
print("less than 100")
} else if (var_a > 200) {
print("greater than 200")
} else {
print("otherwise")
}