Operators
## Learning Objectives
- Master arithmetic operators
- Understand comparison operators
- Learn logical operators
- Work with vectorization
## Arithmetic Operators
### Basic Operations
```r
a <- 10
b <- 3
a + b # 13 (addition)
a - b # 7 (subtraction)
a * b # 30 (multiplication)
a / b # 3.33... (division)
a ^ b # 1000 (power)
a %% b # 1 (modulus)
a %/% b # 3 (integer division)
```
### Division Behavior
```r
# Standard division
10 / 3 # 3.333333
# Integer division (floor)
10 %/% 3 # 3
# Modulus (remainder)
10 %% 3 # 1
```
### Increment/Decrement
```r
# R doesn't have ++ operator, use this instead
x <- 5
x <- x + 1 # 6
x <- x - 1 # 5
# Or use compound assignment
x <- 5
x += 1 # 6
x -= 1 # 5
x *= 2 # 10
x /= 2 # 5
```
## Comparison Operators
### Relational Operators
```r
a <- 5
b <- 10
a == b # FALSE (equal)
a != b # TRUE (not equal)
a < b # TRUE (less than)
a > b # FALSE (greater than)
a <= b # TRUE (less or equal)
a >= b # FALSE (greater or equal)
```
### Comparison with NA
```r
# NA comparisons need special handling
x <- c(1, 2, NA, 4)
# This returns NA for NA comparison
x == NA
# Use is.na() instead
is.na(x) # FALSE FALSE TRUE FALSE
# Filter using complete.cases
x[!is.na(x)] # 1 2 4
```
## Logical Operators
### AND, OR, NOT
```r
a <- TRUE
b <- FALSE
!a # FALSE (NOT)
a & b # FALSE (AND - element-wise)
a && b # FALSE (AND - first element only)
a | b # TRUE (OR - element-wise)
a || b # TRUE (OR - first element only)
```
### Element-wise vs Single-value
```r
# Single values use && and ||
if (TRUE && FALSE) {
print("Won't print")
}
# Vectors use & and |
c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)
# TRUE FALSE FALSE
```
### Short-Circuit Evaluation
```r
# && and || short-circuit
x <- 0
# Won't evaluate y / x because x != 0 is FALSE
if (x != 0 && y / x > 2) {
"yes"
}
# || short-circuits on TRUE
if (FALSE || TRUE) {
"yes"
}
```
### Truth Table
| A | B | A & B | A \| B | !A |
|---|---|-------|--------|-----|
| TRUE | TRUE | TRUE | TRUE | FALSE |
| TRUE | FALSE | FALSE | TRUE | FALSE |
| FALSE | TRUE | FALSE | TRUE | TRUE |
| FALSE | FALSE | FALSE | FALSE | TRUE |
## Assignment Operators
### Variable Assignment
```r
# Standard assignment
x <- 10
# Also works but not idiomatic
x = 10
# Global assignment
x <<- 10
# For functions, can use
assign("x", 10)
```
## Special Operators
### Sequence Operator
```r
1:5 # c(1, 2, 3, 4, 5)
5:1 # c(5, 4, 3, 2, 1)
-3:3 # c(-3, -2, -1, 0, 1, 2, 3)
```
### Membership Operator
```r
# %in% checks membership
x <- 3
x %in% c(1, 2, 3, 4, 5) # TRUE
# Useful for filtering
values <- c("apple", "banana", "cherry")
"banana" %in% values # TRUE
values %in% c("banana", "orange") # FALSE TRUE FALSE
```
### String Paste Operator
```r
# %% for string concatenation
name <- "Alice"
paste("Hello", name) # "Hello Alice"
# paste0 (no separator)
paste0("Hello", name) # "HelloAlice"
# sprintf style
sprintf("Hello %s, you have %d messages", name, 5)
```
### Other Special Operators
```r
# %/% integer division
10 %/% 3 # 3
# %% modulus
10 %% 3 # 1
# %*% matrix multiplication
mat <- matrix(1:4, nrow = 2)
mat %*% mat # Matrix product
```
## Operator Precedence
### Highest to Lowest
| Priority | Operators |
|----------|-------------------|
| 1 | `::`, `:::` |
| 2 | `[`, `[[` |
| 3 | `$`, `@` |
| 4 | `^` (unary) |
| 5 | `:` (sequence) |
| 6 | `::` |
| 7 | `-`, `+` (unary) |
| 8 | `::` |
| 9 | `%%`, `%/%`, `%*%` |
| 10 | `*`, `/` |
| 11 | `+`, `-` (binary) |
| 12 | `<`, `>`, `<=`, `>=`, `==`, `!=` |
| 13 | `!` |
| 14 | `&`, `&&` |
| 15 | `\|`, `\|\|` |
| 16 | `~` |
| 17 | `->`, `->>` |
| 18 | `<-`, `<<-` |
| 19 | `=` |
| 20 | `?` (help) |
### Use Parentheses
```r
# Clear precedence
(a + b) * c
# Instead of relying on memory
a + b * c # Multiplies first
```
## Vectorization
### What is Vectorization?
R performs operations element-wise on vectors.
```r
# Element-wise addition
c(1, 2, 3) + c(10, 20, 30) # c(11, 22, 33)
# This is more efficient than loops
vec <- 1:1000
vec2 <- vec * 2 # No loop needed
```
### Vectorized Functions
```r
# Many functions are naturally vectorized
sqrt(c(1, 4, 9, 16)) # c(1, 2, 3, 4)
log(c(1, 10, 100)) # c(0, 2.3, 4.6)
```
### Recycling
```r
# Shorter vector is recycled
c(1, 2) + c(10, 20, 30, 40)
# Warning: longer object length not multiple of shorter
# But with single value
c(1, 2, 3) + 10 # c(11, 12, 13)
```
### Comparison with Loops
```r
# Vectorized (fast)
result <- sum(1:1000000)
# Loop version (slow in R)
total <- 0
for (i in 1:1000000) {
total <- total + i
}
```
### Vectorized ifelse
```r
# ifelse for vectorized conditionals
x <- 1:10
ifelse(x > 5, "big", "small")
# "small" "small" "small" "small" "small" "big" "big" "big" "big" "big"
# Nested ifelse
score <- 75
ifelse(score >= 90, "A",
ifelse(score >= 80, "B",
ifelse(score >= 70, "C", "F")))
# "C"
```
## Summary
- Arithmetic: `+`, `-`, `*`, `/`, `^`, `%%`, `%/%`
- Comparison: `==`, `!=`, `<`, `>`, `<=`, `>=`
- Logical: `&`, `|`, `!`, `&&`, `||`
- Assignment: `<-`, `<<-`, `=`
- Special: `%in%`, `%%`, `%*%`, `%/%`, `:` (sequence)
- Use parentheses to clarify precedence
- R is vectorized - operations apply element-wise
- Use `ifelse()` for vectorized conditionals
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →