Beginner

Control Flow

Master conditional statements, loops, the apply family, and vectorized operations - the building blocks of R program logic.

if / else if / else

R
temperature <- 28

if (temperature > 30) {
  print("It's hot!")
} else if (temperature > 20) {
  print("It's warm.")
} else {
  print("It's cool.")
}
# "It's warm."

# Inline ifelse (vectorized)
x <- c(5, 15, 25)
ifelse(x > 10, "big", "small")
# "small" "big" "big"

Comparison Operators

OperatorDescriptionExample
==Equal to5 == 5 → TRUE
!=Not equal to5 != 3 → TRUE
>Greater than5 > 3 → TRUE
<Less than3 < 5 → TRUE
>=Greater than or equal5 >= 5 → TRUE
<=Less than or equal3 <= 5 → TRUE

Logical Operators

OperatorDescriptionExample
&Element-wise ANDTRUE & FALSE → FALSE
|Element-wise ORTRUE | FALSE → TRUE
!NOT!TRUE → FALSE
&&Short-circuit AND (scalars)TRUE && FALSE → FALSE
||Short-circuit OR (scalars)FALSE || TRUE → TRUE

for Loops

R
# Basic for loop
for (i in 1:5) {
  print(i)
}

# Loop over a vector
fruits <- c("apple", "banana", "cherry")
for (fruit in fruits) {
  print(paste("I like", fruit))
}

# Loop with index
for (i in seq_along(fruits)) {
  print(paste(i, fruits[i]))
}

while Loops

R
count <- 1
while (count <= 5) {
  print(count)
  count <- count + 1
}

repeat and break

R
# repeat runs forever until break is called
x <- 1
repeat {
  print(x)
  x <- x + 1
  if (x > 5) break
}

next (Skip Iteration)

R
# next is R's equivalent of "continue" in other languages
for (i in 1:10) {
  if (i %% 2 == 0) next  # Skip even numbers
  print(i)
}
# 1 3 5 7 9

The apply Family

R's apply family of functions replaces many loops with more idiomatic, often faster alternatives:

R
# sapply - apply a function to each element, simplify result
sapply(1:5, function(x) x^2)
# 1  4  9 16 25

# lapply - like sapply but always returns a list
lapply(1:3, function(x) x * 10)
# [[1]] 10  [[2]] 20  [[3]] 30

# apply - apply function over matrix rows (1) or columns (2)
mat <- matrix(1:12, nrow = 3)
apply(mat, 1, sum)   # Row sums
apply(mat, 2, mean)  # Column means

# tapply - apply by group
scores <- c(85, 90, 78, 92, 88)
groups <- c("A", "B", "A", "B", "A")
tapply(scores, groups, mean)
# A: 83.67  B: 91.00

Vectorized Operations

In R, prefer vectorized operations over loops whenever possible - they are faster and more readable:

R
# AVOID: loop to square each element
nums <- 1:1000000
result <- numeric(length(nums))
for (i in seq_along(nums)) {
  result[i] <- nums[i]^2
}

# PREFER: vectorized operation
result <- nums^2  # Same result, much faster!

# More vectorized examples
x <- c(1, 2, 3, 4, 5)
x > 3                    # FALSE FALSE FALSE TRUE TRUE
sum(x > 3)               # 2 (count of TRUE values)
x[x > 3]                 # 4 5 (filtering)
cumsum(x)                 # 1 3 6 10 15
R Idiom: "If you're using a for loop in R, there's probably a better way." Vectorized operations and the apply family are the R way of doing things.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.