| 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) |
(Sections 2.6 - 2.7)
| 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) |
Recycling applies in vector arithmetic (as in most of R):
%/%) and Remainder (%%)R provides two special operations that are related to division:
| 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?
Let’s review the ideas of quotient and remainder:
The %/% operator finds tha quotient for you:
The %% operator in R finds the remainder:
%%Look at these numbers:
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:
Second, select from vec with logical sub-setting:
Of course it is perfectly fine to do it all in one line:
We would pronouce this as: “vec, where vec mod 6 is 0”.
Remember that you can take square roots with sqrt():
sqrt() works on numerical vectors of any length:
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.
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:
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:
Or use the special mean() function:
The max() function delivers the maximum value of the elements of a numerical vector:
The min() function delivers the minimum value of a numerical vector:
The pmax() function compares corresponding elements of each input-vector and produces a vector of the maximum values:
There is a pmin() function that computes pair-wise minima as well.
NA is Contagious!This is especially a problem for “single-number” functions:
NAsYou can pick out NAs with the is.na() function (already covered).
But for some functions you can use a na.rm parameter:
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:
yellowBrickRoadyellow_brick_roadyellow.brick.roadyell23y2e3L45Lo3....rOAD.yellowThe 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?
Each new word-like part of the variable names begins with a capital letter. (The initial letter, though, is often not capitalized.) Examples:
emeraldCityisEvenUse lowercase and to separate words with underscores:
emerald_cityis_even. as SeparatorAn older convention was to separate words with dots:
emerald.cityis.evenNo longer recommended. (In other computer languages the dot has a special meaning.)
In each program use or either camelCase or snake_case, but not both.
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.
Variable names, together with reserved words, make up the set of identifiers in a program.