- 1
- We create a vector with letters a-d
We went over some operators in our lesson on Basics
Basic math operators:
| Operation | Written | In R |
|---|---|---|
| Addition | \(1+1\) | 1+1 |
| Subtraction | \(2-1\) | 2-1 |
| Multiplication | \(3\times 2\) or \(3\cdot 2\) | 3*2 |
| Division | \(3 / 2\) or \(\dfrac{3}{2}\) | 3/2 |
| Exponential | \(4^2\) | 4^2 |
| Square root | \(\sqrt{4}\) or \(4^{1/2}\) | 4^(1/2) or sqrt(4) |
Basic relational operators:
| Operation | In R |
|---|---|
| Less than | 1<1 |
| Greater than | 1>1 |
| Less than or equal to | 1<=1 |
| Greater than or equal to | 1>=1 |
| Equal to | 1==1 |
| Not equal to | 1!=1 |
| Operation | In R | Example in R | Example in plain language |
|---|---|---|---|
| Group criteria | ( ) |
(1 == 1) |
We are grouping this statement to make it easier to read |
| And | & |
(1 == 1) & (3 > 2) |
Is 1 equal to 1 AND 3 greater than 2? (If BOTH are true, returns true) |
| Or | | |
(1 == 1) | (1 > 2) |
Is 1 equal to 1 OR is 1 greater than 2? (If one of them are true, returns TRUE) |
| Not | ! |
!(1 > 2) |
Is 1 NOT greater than 2? |
Adapted from this R for Data Science
The %in% operator is helpful when you want to check if one thing is in a list of other things
Let’s say I have a vector
[1] TRUE
| Purpose | Function | Example |
|---|---|---|
| rounding | round(x, digits = n) |
|
| rounding | janitor::round_half_up(x, digits = n) |
janitor::round_half_up(3.5, digits = 0) |
| ceiling (round up) | ceiling(x) |
ceiling(3.1234) |
| floor (round down) | floor(x) |
floor(3.1234) |
| absolute value | abs(x) |
abs(3.1234) |
| square root | sqrt(x) |
sqrt(3.1234) |
| exponent | exponent(x) |
exponent(3.1234) |
| natural logarithm | log(x) |
log(3.1234) |
| log base 10 | log10(x) |
log10(3.1234) |
| log base 2 | log2(x) |
log2(3.1234) |
x is a vector of numeric values, then we can perform several statistical functions on it| Objective | Function |
|---|---|
| mean (average) | mean(x, na.rm=T) |
| median | median(x, na.rm=T) |
| standard deviation | sd(x, na.rm=T) |
| quantiles* | quantile(x, probs) |
| sum | sum(x, na.rm=T) |
| minimum value | min(x, na.rm=T) |
| maximum value | max(x, na.rm=T) |
| range of numeric values | range(x, na.rm=T) |
| summary** | summary(x) |
CAUTION: The functions above will by default include missing values in calculations. Missing values will result in an output of NA, unless the argument na.rm = TRUE is specified. This can be written shorthand as na.rm = T.
| Objective | Function | Example |
|---|---|---|
| create a sequence | seq(from, to, by) |
seq(1, 10, 2) |
| repeat x, n times | rep(x, ntimes) |
rep(1:3, 2) or rep(c("a", "b", "c"), 3) |
| take a random sample | sample(x, size) |
sample(1:15, size = 5, replace = TRUE) |
Extra note: we will “set a seed” for sample() because the process is random, and the output will change everytime. set.seed() will anchor us in a specific instance of the random number generator.
Key operators and functions