R Vector Operations

Vector operations let you perform calculations on entire collections of values at once without writing loops. This is one of R's most powerful features — the same operation that would require a loop in most languages runs in a single line in R.

Vectorized Arithmetic

a <- c(10, 20, 30, 40, 50)
b <- c(1, 2, 3, 4, 5)

a + b      # 11 22 33 44 55
a - b      # 9 18 27 36 45
a * b      # 10 40 90 160 250
a / b      # 10 10 10 10 10
a ^ b      # 10 400 27000 2560000 312500000

# Operations with a single value
a * 2      # 20 40 60 80 100
a + 100    # 110 120 130 140 150

Recycling Explained

a <- c(10, 20, 30, 40, 50, 60)
b <- c(1, 2)   # shorter vector

a + b
# b gets recycled: 1 2 1 2 1 2
# Result: 11 22 31 42 51 62

Diagram:
a:  10  20  30  40  50  60
b:   1   2   1   2   1   2   ← recycled
     ─   ─   ─   ─   ─   ─
+:  11  22  31  42  51  62

R gives a warning when the longer vector's length is not a multiple of the shorter one.

Comparison Operations

temps <- c(22, 35, 18, 40, 28)

temps > 30         # FALSE TRUE FALSE TRUE FALSE
temps == 18        # FALSE FALSE TRUE FALSE FALSE
temps >= 22 & temps <= 35  # TRUE TRUE FALSE FALSE TRUE

Set Operations on Vectors

x <- c(1, 2, 3, 4, 5)
y <- c(3, 4, 5, 6, 7)

union(x, y)        # 1 2 3 4 5 6 7
intersect(x, y)    # 3 4 5
setdiff(x, y)      # 1 2  (in x but not y)
setdiff(y, x)      # 6 7  (in y but not x)
Set Diagram:
  x: {1, 2, 3, 4, 5}
  y:         {3, 4, 5, 6, 7}
             ─────
  intersect:  3, 4, 5
  x setdiff:  1, 2
  y setdiff:              6, 7
  union:     1, 2, 3, 4, 5, 6, 7

Statistical Functions on Vectors

data <- c(45, 78, 92, 63, 55, 88, 71)

sum(data)      # 492
mean(data)     # 70.28571
median(data)   # 71
var(data)      # 289.2381  (variance)
sd(data)       # 17.0069   (standard deviation)
range(data)    # 45 92 (min and max)
diff(data)     # differences between consecutive elements
cumsum(data)   # cumulative sum
cumprod(data)  # cumulative product

Sorting and Ranking

scores <- c(85, 72, 91, 64, 88)

sort(scores)              # 64 72 85 88 91 (ascending)
sort(scores, decreasing = TRUE)  # 91 88 85 72 64
order(scores)             # 4 2 1 5 3 (indices that would sort it)
rank(scores)              # 3 2 5 1 4 (rank of each element)
rev(scores)               # 88 64 91 72 85 (reverse order)

String Operations on Character Vectors

names <- c("alice", "bob", "charlie", "diana")

toupper(names)           # "ALICE" "BOB" "CHARLIE" "DIANA"
nchar(names)             # 5 3 7 5
paste(names, "Singh")    # "alice Singh" "bob Singh" ...
grepl("a", names)        # TRUE FALSE TRUE TRUE (contains "a"?)

Applying a Custom Function to a Vector

prices <- c(100, 250, 180, 320)

# Apply discount: 10% off items over 200, else 5% off
discounts <- ifelse(prices > 200, prices * 0.10, prices * 0.05)
final <- prices - discounts

print(final)   # 95 225 171 288

Vectorized operations are what make R fast. When working with data frames later in this course, you will apply these same vector operations to entire columns — processing thousands of rows in microseconds without a single explicit loop.

Leave a Comment

Your email address will not be published. Required fields are marked *