Vectors
A vector is R's most fundamental data structure — an ordered collection of values of the same type, like a single column of numbers, that R can operate on all at once.
Learn Vectors in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll create vectors with c() and sequences, pick elements out by position and by condition, and use vectorized math to transform whole vectors in a single line.
What You'll Learn in This Lesson
1️⃣ Creating Vectors
The workhorse is c() , which combines values into a vector. For regular patterns, 1:5 makes a sequence, seq() gives you control over the step, and rep() repeats values.
2️⃣ Indexing: Picking Elements
Use square brackets to pull out elements. R indexes from 1 . You can ask for a single position, several positions, a range, everything except some positions (with a minus), or — most powerfully — every element that passes a logical test.
That last line — scores[scores >= 90] — is the pattern you'll use constantly: filter a vector down to the values that meet a condition.
3️⃣ Vectorized Math
Operations apply to every element automatically — no loop required. You can combine a vector with a single number (R repeats the number) or with another vector (element by element). Summary functions like sum() and mean() collapse a vector to one value.
Your turn. Fill in the # TODO blank, run it, and compare with the expected output.
Mini-Challenge: Weekly Steps
Write it from the outline, run it, and check it against the example output. The sum(steps > 8000) trick is worth remembering — comparing a vector gives TRUE/FALSE, and summing them counts the TRUEs.
Common Errors (and the fix)
- Off-by-one from 0-indexing habits — x[0] returns an empty vector, not the first element. The first element is x[1] .
- Numbers turned into text — c(1, 2, "3") coerces everything to character. Keep one type per vector; check with class() .
- "longer object length is not a multiple..." — a recycling warning when two vectors don't divide evenly. Make their lengths match, or recycle on purpose.
- NA spreading through math — if a vector contains NA , mean(x) returns NA. Use mean(x, na.rm = TRUE) to skip missing values.
Pro Tips
- 💡 Think in whole vectors: if you're tempted to write a loop, ask whether a vectorized operation does it in one line.
- 💡 Name your elements: c(a = 1, b = 2) lets you index by name — x["a"] .
- 💡 Count conditions fast: sum(x > 10) counts matches, mean(x > 10) gives the proportion.
📋 Quick Reference — Vectors
Task
R Syntax
Create a vector
c(1, 2, 3)
Sequence
seq(0, 10, by = 2)
First element
x[1]
Filter by condition
x[x > 5]
Count of elements
length(x)
Sum / average
sum(x); mean(x)
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ A vector is an ordered collection of one type, built with c()
- ✅ R indexes from 1 using square brackets
- ✅ Logical indexing — x[x > 5] — filters a vector
- ✅ Math is vectorized : it applies to every element at once
- ✅ sum() , mean() , max() summarise a vector
- ✅ Next lesson: Operators — the symbols that drive these comparisons
Practice quiz
Which function combines values into a vector in R?
- vector()
- c()
- list()
- combine.all()
Answer: c(). c() ('combine') builds a vector from individual values.
What does 1:5 produce?
- c(1, 2, 3, 4, 5)
- c(1, 5)
- c(0, 1, 2, 3, 4)
- An error
Answer: c(1, 2, 3, 4, 5). The colon makes the integer sequence 1, 2, 3, 4, 5.
What does seq(0, 10, by = 2) return?
- c(0, 10)
- c(2, 4, 6, 8, 10)
- c(0, 5, 10)
- c(0, 2, 4, 6, 8, 10)
Answer: c(0, 2, 4, 6, 8, 10). seq() steps from 0 to 10 in increments of 2.
At what index does R start counting?
- At 0
- At -1
- At 1
- At the last element
Answer: At 1. R is 1-indexed: x[1] is the first element.
What does scores[scores >= 90] do?
- Counts elements >= 90
- Keeps only the elements that are 90 or greater
- Returns positions
- Returns TRUE/FALSE
Answer: Keeps only the elements that are 90 or greater. Logical indexing keeps elements where the test is TRUE.
What does length(ages) return?
- The number of elements in the vector
- The sum of the elements
- The largest element
- The vector itself
Answer: The number of elements in the vector. length() counts how many elements a vector has.
What does c(1, "a") become?
- A list
- An error
- Numeric
- A character vector (everything coerced to character)
Answer: A character vector (everything coerced to character). A vector holds one type; R coerces to the most flexible type, here character.
What does c(1, 2, 3, 4) + c(10, 20) give?
- c(11, 22)
- An error
- c(11, 22, 13, 24)
- c(11, 22, 33, 44)
Answer: c(11, 22, 13, 24). R recycles the shorter vector: 1+10, 2+20, 3+10, 4+20.
What does sum(x > 8000) compute?
- The total of all values
- The count of elements greater than 8000
- The mean of x
- The max of x
Answer: The count of elements greater than 8000. Comparing gives TRUE/FALSE; summing counts the TRUEs.
What does x[0] return in R?
- The first element
- An error
- NULL
- An empty vector
Answer: An empty vector. Index 0 selects nothing, returning a length-0 (empty) vector.
Continue this course
- Previous: RStudio & Running Code
- Next: Operators