Mathematical Operators

Arithmetic

All of the familiar arithmetic operators are available in R. When we do arithmetic on named R objects, the values stored in those objects are used to evaluate the expression.

The following code will add, subtract, multiply, and divide the values stored as y and x.

## First, define a few objects to use below.
x <- 2.5
y <- 7
z <- 5

## Show some basic arithmetic operations
y + x
[1] 9.5
y - x
[1] 4.5
y * x
[1] 17.5
y / x
[1] 2.8

Powers & Roots

We use the caret character, ^, to specify exponents. The following code will square and cube the value of y.

y^2
[1] 49
y^3
[1] 343

The sqrt() function returns the square root of it’s argument (i.e., the value we specify inside the parentheses). The following code calculates the square root of y.

sqrt(y)
[1] 2.645751

For other roots, we can use fractional exponents.

y^(1/3) # cube root of y
[1] 1.912931
y^(1/4) # quartic root of y
[1] 1.626577

Other Common Operators

R also includes many special functions for the most common mathematical operations.

log(y)   # natural logarithm of y
[1] 1.94591
log10(y) # log base 10 of y
[1] 0.845098
log2(y)  # log base 2 of y
[1] 2.807355
exp(x) # exponentiate x
[1] 12.18249
y %% x # modulo: remainder after dividing y by x
[1] 2
foo <- -3.14159

round(foo, 3) # round 'foo' to 3 decimal places
[1] -3.142
floor(foo)    # round 'foo' down to the nearest whole number
[1] -4
ceiling(foo)  # round 'foo' up to the nearest whole number
[1] -3
abs(foo) # absolute value of 'foo'
[1] 3.14159
Note

Note that log(y) calculates the natural logarithm of y, \(\ln(y)\). If you want the “ordinary” base-10 log, you need to use the log10() function.

Practice
  1. Create an object called age that takes the value of your age in whole years.
  2. Use the age object you created in above to create a second object called weeks that takes the value of your age in whole weeks.
    • Assume 52 weeks in each year.
    • Disregard partial years (i.e., assume every year counted in age contains 52 whole weeks).
  3. Print the value of weeks.

At time-of-writing, I’m 38 years old. So, these would be my age and weeks objects.

age   <- 38
weeks <- 38 * 52
weeks
[1] 1976

Order of Operations

When parsing your commands, R will, mostly, scan each line of code from left to right and apply each mathematical operation according to the usual PEMDAS ordering.

Notice how the following two expressions produce different results.

  • In the first line, we first divide x by z and then add y to the result.
  • In the second line, we first add x to y and then divide the result by z.
y + x / z
[1] 7.5
(y + x) / z
[1] 1.9
Practice

Why do the following two expressions produce different answers?

y^(1/2)
[1] 2.645751
y^1/2
[1] 3.5
y^(1/2)
[1] 2.645751
sqrt(y)
[1] 2.645751

In the first case, we’re raising y to the power of \(\frac{1}{2}\).

y^1/2
[1] 3.5
y/2
[1] 3.5

In the second case, we’re first raising y to the power of 1 (which does nothing) and then dividing the result by 2.

Back to top