6  Mathematical operations and comparisons

6.1 Quiz: Mathematical operations I

How to get the reminder of 4.5 divided by 2.5?

✗4.5^2.5

✗4.5 / 2.5

✓4.5 %% 2.5

✗4.5 %/% 2.5

✗4.5 \ 2.5

The %% gives the reminder of the division.

6.2 Quiz: Mathematical operations II

When you execute the following operation in R

3*2^2

the result will be:

✗an error

✓12

✗36

✗NA

Recall that R uses the PEMDAS precendence standard when evaluating mathematical expressions.

R uses the PEMDAS precedence standard, so the exponent will be evaluated first and then the multiplication.

6.3 Quiz: Comparisons I

To compare if three is greater or equal than four you should write:

✗3 > 4

✗3 >> 4

✗3 => 4

✓3 >= 4

>= is the greater than operator in R.

6.4 Quiz: Comparisons II

The result of executing

a <- 1:5  
(a < 2) | (3 > 2) 

will be:

✓TRUE TRUE TRUE TRUE TRUE

✗an error

✗TRUE

✗FALSE

Recall that a is a vector.

Given that a is a vector, this will be a vectorized comparion, i.e. the comparison will be performed for each element of a, and the result is a vector of booleans.

6.5 Quiz: Comparisons III

The result of executing

("z" < "a") > 2

will be:

✗TRUE

✓FALSE

✗NA

✗an error

Recall that you can compare the order of characters in the alphabet.

First what is inside the parenthesis is evaluated: ("z" < "a"), the result is FALSE, and then the resulting expression is evaluated FALSE > 2. In order for the expression to be evaluated R is going to convert FALSE to a numeric value, 0, then evaluate the expression 0 > 2.