Arithmetic, Variable-Names

(Sections 2.6 - 2.7)

Some Arithmetic Operations

Operation What It Means
x + y addition
x - y subtraction
x * y multiplication
x / y division
x^y exponentiation (raise x to the power y)

Operations Work Element-Wise

x <- c(10, 15, 20)
y <- c(3, 4, 5)
x + y
[1] 13 19 25
x - y
[1]  7 11 15
x * y
[1]  30  60 100
x / y
[1] 3.333333 3.750000 4.000000
x^y
[1]    1000   50625 3200000

Recycling Applies

Recycling applies in vector arithmetic (as in most of R):

vec <- c(2, 7, 9, 12, 15, 24)
2 * vec  # the 2 will be recycled
[1]  4 14 18 24 30 48
vec + 100 # the 100 will be recycled
[1] 102 107 109 112 115 124
vec^3  # the 3 will be recycled
[1]     8   343   729  1728  3375 13824

Quotient (%/%) and Remainder (%%)

R provides two special operations that are related to division:

Basic arithmetical operations on vectors.
Operation What It Means
x %/% y integer division (quotient after dividing x by y)
x %% y x mod y (remainder after dividing x by y)

What do they do?

Review of Division

Let’s review the ideas of quotient and remainder:

  • Suppose you divide 17 by 5.
  • The quotient is 3 …
    • … because \(3 \times 5 = 15 \leq 17\), but
    • \(4 \times 5 = 20 > 17\).
  • The remainder is 2 …
    • … because \(17 - 15 = 2\).

Let R Do It

The %/% operator finds tha quotient for you:

17 %/% 5
[1] 3

The %% operator in R finds the remainder:

## write the dividend, then %%, then the divisor:
17 %% 5
[1] 2

An Application of %%

Look at these numbers:

vec <- 5:20
vec
 [1]  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

Challenge

Find all the numbers in vec that are multiples of 6. Two-steps:

First, find out whether or not each number has a remainder of 0 when divided by 6:

is_multiple_of_6 <- vec %% 6 == 0
is_multiple_of_6
 [1] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
[13] FALSE  TRUE FALSE FALSE

Second Step

Second, select from vec with logical sub-setting:

vec[is_multiple_of_6]
[1]  6 12 18

All at Once

Of course it is perfectly fine to do it all in one line:

vec[vec %% 6 == 0]
[1]  6 12 18

We would pronouce this as: “vec, where vec mod 6 is 0”.

More Examples

vec[vec %% 2 == 0] # just the even numbers
[1]  6  8 10 12 14 16 18 20
vec[vec %% 2 == 1] # just the odd numbers
[1]  5  7  9 11 13 15 17 19
vec[vec %% 3 == 1] # numbers one more than a multiple of 3
[1]  7 10 13 16 19

Square Roots

Remember that you can take square roots with sqrt():

sqrt(9)
[1] 3

Lots of Roots

sqrt() works on numerical vectors of any length:

sqrt(1:10)  ## square roots of the first ten numbers
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427
 [9] 3.000000 3.162278

Vectorization

sqrt(), %% and many other R-functions implement vectorization.

Vectorization

R’s ability to operate on each element of a vector, producing a new vector of the same length.

Vectorized operations can be expressed concisely and performed very quickly.

Summing

Some R-functions take a vector and return only only a vector of length one (a “single number”).

For example, sum() returns the sum of the elements:

one_to_hundred <- 1:100
sum(one_to_hundred)
[1] 5050

Mean

The mean is defined as:

\[\frac{\text{sum of the numbers}}{\text{how many numbers there are}}\] You can find the mean of a numerical vector as follows:

vec <- c(-3, 4, 17, 23, 51)
sum(vec)/length(vec)
[1] 18.4

Or use the special mean() function:

mean(vec)
[1] 18.4

Max and Min

The max() function delivers the maximum value of the elements of a numerical vector:

max(c(3, 7, 2))
[1] 7

The min() function delivers the minimum value of a numerical vector:

min(c(3, 7, 2))
[1] 2

Pairwise Max and Min

The pmax() function compares corresponding elements of each input-vector and produces a vector of the maximum values:

a <- c(3, 7, 10)
b <- c(5, 2, 12)
pmax(a, b)
[1]  5  7 12

There is a pmin() function that computes pair-wise minima as well.

Remember: NA is Contagious!

vec <- c(1, 2, 3, 4, NA)
sqrt(vec)
[1] 1.000000 1.414214 1.732051 2.000000       NA

This is especially a problem for “single-number” functions:

vec <- c(1, 2, 3, 4, NA)
sum(vec)
[1] NA
mean(vec)
[1] NA

Removing NAs

You can pick out NAs with the is.na() function (already covered).

But for some functions you can use a na.rm parameter:

vec <- c(1, 2, 3, 4, NA)
sum(vec, na.rm = TRUE)
[1] 10
mean(vec , na.rm = TRUE)
[1] 2.5

Variable Names and Reserved Words

What Can Be a Variable Name?

According to R’s documentation:

“A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number.”

These are all legal variable names:

  • yellowBrickRoad
  • yellow_brick_road
  • yellow.brick.road
  • yell23
  • y2e3L45Lo3....rOAD
  • .yellow

But Not Everything is OK

The following are not legal variable names:

  • .2yellow (cannot start with dot and then number)
  • _yellow (cannot start with _)

What’s a good way to make up variable names?

One Good Practice: camelCase

Each new word-like part of the variable names begins with a capital letter. (The initial letter, though, is often not capitalized.) Examples:

  • emeraldCity
  • isEven

Another Good Practice: snake_case

Use lowercase and to separate words with underscores:

  • emerald_city
  • is_even

A BAD Practice: . as Separator

An older convention was to separate words with dots:

  • emerald.city
  • is.even

No longer recommended. (In other computer languages the dot has a special meaning.)

Reserved Words

Reserved Words

Identifiers that are set aside by R for specific programming purposes. They cannot be used as names of variables.

The following are reserved for special purposes in R:

if, else, while, repeat, function, for, in, next, break,TRUE, FALSE, NULL, inf, NaN, NA, NA_integer, NA_real, NA_complex, NA_character

You cannot use them as names for variables.

Identifiers

Variable names, together with reserved words, make up the set of identifiers in a program.