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
| Operator | Description | Example |
|---|---|---|
== | Equal to | 5 == 5 → TRUE |
!= | Not equal to | 5 != 3 → TRUE |
> | Greater than | 5 > 3 → TRUE |
< | Less than | 3 < 5 → TRUE |
>= | Greater than or equal | 5 >= 5 → TRUE |
<= | Less than or equal | 3 <= 5 → TRUE |
Logical Operators
| Operator | Description | Example |
|---|---|---|
& | Element-wise AND | TRUE & FALSE → FALSE |
| | Element-wise OR | TRUE | 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.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX