## Define some objects to play with
<- 5
y <- 7
x <- 5
w
### Check equality
== x y
[1] FALSE
== w y
[1] TRUE
We can tests many flavors of logical conditions in R. Logical tests return a logical vector as the result. A logical vector takes the value of TRUE
whenever the tested condition is satisfied and FALSE
when the tested condition is not satisfied.
The simplest logical test is an equality check. To check if two objects are equal, we use the ‘equality operator’, ==
.
We can also check the usual greater-than/less-than conditions with >
, <
, >=
, <=
.
[1] FALSE
[1] FALSE
[1] TRUE
[1] TRUE
We can negate any logical condition by prepending the ‘!’ character
Rather than negating an equality check, we will typically use the special “not-equal” operator, !=
.
We can create more complex logical conditions with the AND and OR operators: &
and |
.
Use a single line of code to generate a logical value (i.e., TRUE/FALSE) indicating if the value of the ‘weeks’ object you created above is evenly divisible by 7.
x %% 7
to calculate the remainder after dividing x
by 7
.%%
, takes precedence over arithmetic operations like multiplication.