What Is a Vector?
A vector is the most basic data structure in R: an ordered, one-dimensional collection of values that all share the same atomic type — logical, integer, double, character, or complex. Even a single number like 5 is technically a vector of length one. Vectors are created most commonly with the c() (combine) function, and R's design treats almost every operation as vectorized, meaning functions and arithmetic operators act on entire vectors element by element without explicit loops.
Cricket analogy: A batting scorecard listing every run Virat Kohli scored ball by ball across an innings is one ordered sequence of same-type values (runs), just like a vector holds same-typed elements in order.
Creating and Indexing Vectors
Vectors can be built with c() for arbitrary values, seq() for numeric sequences with a defined step (seq(1, 10, by = 2)), rep() for repeating patterns (rep(c(1,2), times = 3)), and the colon operator for consecutive integers (1:10). Once created, elements are accessed with square brackets: positive indices select elements by position (v[3]), negative indices exclude positions (v[-1]), logical vectors select elements where the condition is TRUE (v[v > 5]), and named vectors can be indexed by name (v['x']).
Cricket analogy: Selecting v[3] from a scores vector is like picking out the third ball of an over on the highlights reel, while v[v>50] filters an entire innings down to only the deliveries that went for a boundary.
# Creating vectors
scores <- c(45, 78, 23, 99, 61)
evens <- seq(2, 20, by = 2)
pattern <- rep(c("A", "B"), times = 3)
# Indexing
scores[2] # 78 (positive index)
scores[-1] # drop the first element
scores[scores > 50] # logical indexing
names(scores) <- c("p1","p2","p3","p4","p5")
scores["p3"] # named indexing
# Vectorized arithmetic + recycling
bonus <- c(5, 10)
scores + bonus # recycles bonus across scores, with a warningVectorized Arithmetic and the Recycling Rule
Arithmetic operators in R (+, -, *, /) apply element by element when two vectors are combined, so c(1,2,3) + c(10,20,30) yields c(11,22,33). When the vectors have different lengths, R applies the recycling rule: the shorter vector is repeated (recycled) until it matches the length of the longer one, and R issues a warning if the longer length is not an exact multiple of the shorter.
Cricket analogy: Adding a fixed strike-rate bonus vector of length 1 to every batter's score is recycling in action — the single bonus value repeats down the whole scorecard, the way c(scores) + 5 adds 5 to every entry.
When the longer vector's length isn't an exact multiple of the shorter one, R still recycles but emits a warning such as 'longer object length is not a multiple of shorter object length' — the result is still computed, just flagged as likely unintentional.
Vector Types and Coercion
Every atomic vector has exactly one type, discoverable with typeof() or class(): logical, integer, double (the default for decimal or whole numbers), or character. R follows an implicit coercion hierarchy — logical < integer < double < character — so combining mixed types with c() silently promotes every element to the 'widest' type present; for example, c(1, TRUE) becomes numeric where TRUE becomes 1, but c(1, 'a') becomes character where 1 becomes '1'.
Cricket analogy: Mixing a boolean out/not-out flag with numeric scores in one vector coerces everything to numbers, similar to how a scorebook entry marked simply 'W' still gets tallied as a numeric wicket count in the final summary.
Because c() silently coerces to the widest type, c(1, 2, 'three') produces a character vector where 1 and 2 become the strings '1' and '2' — arithmetic on this vector will fail or behave unexpectedly until you explicitly convert back with as.numeric().
- A vector is R's basic building block: an ordered, one-dimensional, same-type collection of values.
- c(), seq(), rep(), and : are the main tools for constructing vectors.
- Indexing uses [ ] with positive positions, negative exclusions, logical conditions, or names.
- Arithmetic on vectors is element-wise and vectorized — no explicit loops needed.
- Shorter vectors are recycled to match longer ones during arithmetic, with a warning if lengths don't divide evenly.
- Mixing types in c() coerces every element to the widest type in the order logical < integer < double < character.
Practice what you learned
1. What is the result of c(1, 2, 3) + c(10, 20)?
2. Which is the idiomatic, most concise way to create the integers 1 through 10?
3. What does v[-1] do for a vector v?
4. What is the result of c(1, TRUE, 'x')?
5. Which indexing approach selects all elements greater than 10 in vector v?
Was this page helpful?
You May Also Like
Data Frames in R
Understand R's core tabular data structure — how data.frame combines vectors of equal length into rows and columns for real-world datasets.
Lists and Matrices in R
Compare R's two other core structures — the flexible, heterogeneous list and the strict, two-dimensional matrix — and learn when to use each.
Factors in R
Learn how R represents categorical data with factors — encoding fixed levels efficiently and controlling ordinal comparisons.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics