R - Operators
R - Operators
Section titled “R - Operators”An operator is a symbol that tells the R interpreter to perform a specific mathematical, relational, or logical operation. R has a rich set of built-in operators that are essential for any analysis.
Types of Operators
Section titled “Types of Operators”- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Miscellaneous Operators (including the Pipe)
Arithmetic Operators
Section titled “Arithmetic Operators”These operators perform standard mathematical calculations. When applied to vectors, the operation is performed element-wise.
| Operator | Description | Example |
|---|---|---|
| + | Addition | v <- c(2, 5.5, 6) t <- c(8, 3, 4) print(v + t) # [1] 10.0 8.5 10.0 |
| − | Subtraction | v <- c(2, 5.5, 6) t <- c(8, 3, 4) print(v - t) # [1] -6.0 2.5 2.0 |
| * | Multiplication | v <- c(2, 5.5, 6) t <- c(8, 3, 4) print(v * t) # [1] 16.0 16.5 24.0 |
| / | Division | v <- c(2, 5.5, 6) t <- c(8, 3, 4) print(v / t) # [1] 0.250000 1.833333 1.500000 |
| %% | Modulus (remainder from division) | v <- c(2, 5.5, 6) t <- c(8, 3, 4) print(v %% t) # [1] 2.0 2.5 2.0 |
| %/% | Integer Division | v <- c(2, 5.5, 6) t <- c(8, 3, 4) print(v %/% t) # [1] 0 1 1 |
| ^ | Exponentiation | v <- c(2, 5.5, 6) t <- c(8, 3, 4) print(v^t) # [1] 256.000 166.375 1296.000 |
Relational Operators
Section titled “Relational Operators”These operators compare two vectors element-wise and return a logical vector (TRUE or FALSE).
| Operator | Description | Example |
|---|---|---|
| > | Greater than | v <- c(2, 5.5, 6, 9) t <- c(8, 2.5, 14, 9) print(v > t) # [1] FALSE TRUE FALSE FALSE |
| < | Less than | v <- c(2, 5.5, 6, 9) t <- c(8, 2.5, 14, 9) print(v < t) # [1] TRUE FALSE TRUE FALSE |
| == | Equal to | v <- c(2, 5.5, 6, 9) t <- c(8, 2.5, 14, 9) print(v == t) # [1] FALSE FALSE FALSE TRUE |
| <= | Less than or equal to | v <- c(2, 5.5, 6, 9) t <- c(8, 2.5, 14, 9) print(v <= t) # [1] TRUE FALSE TRUE TRUE |
| >= | Greater than or equal to | v <- c(2, 5.5, 6, 9) t <- c(8, 2.5, 14, 9) print(v >= t) # [1] FALSE TRUE FALSE TRUE |
| != | Not equal to | v <- c(2, 5.5, 6, 9) t <- c(8, 2.5, 14, 9) print(v != t) # [1] TRUE TRUE TRUE FALSE |
Logical Operators
Section titled “Logical Operators”Logical operators work on logical vectors. Non-zero numbers are treated as TRUE, and zero is treated as FALSE. There is a crucial distinction between element-wise (&, |) and short-circuiting (&&, ||) operators.
| Operator | Description | Example |
|---|---|---|
| & | Element-wise AND. Compares each element of two vectors. | v1 <- c(TRUE, TRUE, FALSE, FALSE) v2 <- c(TRUE, FALSE, TRUE, FALSE) print(v1 & v2) # [1] TRUE FALSE FALSE FALSE |
| | | Element-wise OR. Compares each element of two vectors. | v1 <- c(TRUE, TRUE, FALSE, FALSE) v2 <- c(TRUE, FALSE, TRUE, FALSE) print(v1 | v2) # [1] TRUE TRUE TRUE FALSE |
| ! | Logical NOT. Inverts each element of a vector. | v1 <- c(TRUE, FALSE, 0, 6) print(!v1) # [1] FALSE TRUE TRUE FALSE |
| && | Short-circuit AND. Evaluates only the first element of each vector and stops if the first is FALSE. | v <- c(TRUE, FALSE) t <- c(TRUE, TRUE) print(v && t) # Compares v[1] and t[1] only # [1] TRUE |
| || | Short-circuit OR. Evaluates only the first element of each vector and stops if the first is TRUE. | v <- c(FALSE, TRUE) t <- c(FALSE, FALSE) print(v || t) # Compares v[1] and t[1] only # [1] FALSE |
Assignment Operators
Section titled “Assignment Operators”These operators are used to assign values to variables. The R community has strong conventions for which operator to use.
| Operator | Description | Best Practice |
|---|---|---|
| <- | Left Assignment | This is the preferred, idiomatic assignment operator in R. Always use this for assigning values to objects. my_variable <- 10 |
| = | Left Assignment | Works for assignment, but it is stylistically preferred for setting arguments in function calls to avoid ambiguity. mean(x = my_data, na.rm = TRUE) |
| -> | Right Assignment | Assigns the value on the left to the object on the right. It is rarely used as it can harm readability. 10 -> my_variable (Avoid) |
| <<- | Global Assignment | Assigns a value to an object in a parent environment. This can make code very difficult to debug and should be avoided by beginners. It has specific uses inside functions, but these are advanced topics. |
Miscellaneous Operators
Section titled “Miscellaneous Operators”These operators perform specialized tasks, from creating sequences to transforming data pipelines.
| Operator | Description | Example |
|---|---|---|
| : | Colon operator. Creates a sequence of numbers. | v <- 2:8 print(v) # [1] 2 3 4 5 6 7 8 |
| %in% | Value Matching. Checks if elements of the left vector are present in the right vector. | v1 <- c(5, 10, 15) t <- 1:10 print(v1 %in% t) # [1] TRUE TRUE FALSE |
| %*% | Matrix Multiplication. Performs dot-product matrix multiplication. | A <- matrix(1:4, nrow = 2) B <- matrix(c(1, 0, 0, 1), nrow = 2) print(A %*% B) # [,1] [,2] # [1,] 1 3 # [2,] 2 4 |
| |> | Native Pipe. Passes the object on the left as the first argument to the function on the right. Requires R >= 4.1.0. | # Calculate the mean of positive numbers my_data <- c(-1, 0, 5, 10, -5) my_data |> subset(x = _, subset = _ > 0) |> mean() # [1] 7.5 |
| %>% | Magrittr Pipe. The original Tidyverse pipe. More powerful than |> (e.g., with the . placeholder). | library(magrittr) # Get the correlation of two columns in the iris dataset iris %>% subset(Sepal.Length > 5) %>% cor(.$Sepal.Length, .$Petal.Length) # [1] 0.819382 |