CHAPTER 08
Beginner
Vectors in R
Updated: May 18, 2026
5 min read
# CHAPTER 8
Vectors in R
1. Chapter Introduction
Vectors are R's most fundamental data structure — everything in R is built on vectors. Understanding vectors deeply unlocks R's true power: vectorized computation that replaces slow loops with fast, expressive one-liners.2. Creating Vectors
r
3. Vector Indexing
r
4. Vector Operations (Vectorized)
r
5. Mini Project: Student Score Analyzer
r
6. Common Mistakes
-
1-based indexing: R uses 1-based indexing (first element is
x[1], notx[0]).x[0]returnsnumeric(0)— an empty vector!
-
Silent vector recycling:
c(1,2,3,4) + c(10,20)silently recycles toc(11,22,13,24). With non-multiples, R gives a warning but still runs — a common source of subtle bugs.
7. MCQs
Question 1
R vector indexing starts at?
Question 2
x[-c(1,3)] returns?
Question 3
which(x > 5) returns?
Question 4
rep(1:3, each=2) returns?
Question 5
Mixed types in c(1, "two", 3) result in?
Question 6
Vector recycling with non-multiple lengths?
Question 7
order(x) vs sort(x)?
Question 8
quantile(x) returns by default?
Question 9
cut(x, breaks, labels) converts numeric to?
Question 10
x[0] in R returns?
8. Interview Questions
- Q: What is vector recycling in R and when can it cause bugs?
-
Q: What is the difference between
order()andsort()in R?
9. Summary
Vectors: R's fundamental atomic data structure. Create withc(), seq(), rep(). Index with positive (select), negative (exclude), logical (filter). All operations are vectorized — x * 2 multiplies every element. Recycling: shorter vector repeats to match longer. Key functions: sort(), order(), rank(), which(), quantile(). Always use 1-based indexing.