A vector is the most basic and most important data structure in R — an ordered collection of values that all share the same type. Master vectors and most of R starts to make sense.
Creating a Vector
Use the c() function ("combine") to create a vector. Every element inside must be the same type — if you mix types, R will silently convert everything to the most flexible type (usually character).
ages <- c(20, 22, 19, 25) names <- c("Rafi", "Meera", "Zayn") logicals <- c(TRUE, FALSE, TRUE) ages names logicals
[1] 20 22 19 25 [1] "Rafi" "Meera" "Zayn" [1] TRUE FALSE TRUE
c(1, 2, \"three\") produces the character vector \"1\" \"2\" \"three\" — not a mix of numbers and text. R never throws an error here, so this bug can hide in your code for a while.Other Ways to Create Vectors
Besides c(), R gives you quick shortcuts for common patterns — sequences, repeats, and ranges.
1:10 # quick integer sequence 1 to 10 seq(0, 1, by = 0.25) # sequence with a custom step seq(0, 100, length.out = 5) # 5 evenly spaced values rep("A", times = 4) # repeat a value rep(c(1,2), each = 3) # repeat each element 3 times
[1] 1 2 3 4 5 6 7 8 9 10 [1] 0.00 0.25 0.50 0.75 1.00 [1] 0 25 50 75 100 [1] "A" "A" "A" "A" [1] 1 1 1 2 2 2
Indexing — Accessing Elements
R indexing starts at 1, not 0. Use square brackets [ ] to pull out one or more elements.
fruits <- c("apple", "banana", "mango", "grape", "kiwi") fruits[1] # first element fruits[2:4] # a range of elements fruits[c(1,3,5)] # specific positions fruits[-1] # everything EXCEPT the first element fruits[fruits == "mango"] # logical (condition-based) indexing
[1] "apple" [1] "banana" "mango" "grape" [1] "apple" "mango" "kiwi" [1] "banana" "mango" "grape" "kiwi" [1] "mango"
Named Vectors
Vectors can have names attached to each element, which lets you access values by name instead of position.
scores <- c(Math = 90, Physics = 85, Chemistry = 92) scores scores["Physics"] names(scores)
Math Physics Chemistry
90 85 92
Physics
85
[1] "Math" "Physics" "Chemistry"Vectorised Operations & Recycling
Operations apply element-by-element. If the two vectors are different lengths, R "recycles" (repeats) the shorter one — useful, but a common source of silent bugs.
a <- c(1, 2, 3, 4) b <- c(10, 20) a + b # b is recycled: 10,20,10,20
[1] 11 22 13 24
Useful Built-in Functions for Vectors
v <- c(23, 9, 45, 12, 9, 30) length(v) # number of elements sum(v) # total mean(v) # average max(v); min(v) sort(v) # ascending order sort(v, decreasing = TRUE) rev(v) # reverse order unique(v) # remove duplicate values
[1] 6 [1] 128 [1] 21.33333 [1] 45 [1] 9 [1] 9 9 12 23 30 45 [1] 45 30 23 12 9 9 [1] 30 9 12 45 9 23 [1] 23 9 45 12 30
Advanced: Modifying and Filtering Vectors
temps <- c(22, -999, 25, 19, -999, 30) # -999 is a common placeholder for "missing" in old datasets # replace it with NA, the real missing-value marker in R temps[temps == -999] <- NA temps # now compute the mean while ignoring NAs mean(temps, na.rm = TRUE)
[1] 22 NA 25 19 NA 30 [1] 24
Key Points
- A vector holds an ordered set of values that must all be the same type.
- Indexing starts at 1; use
[ ], negative indices, or logical conditions to subset. - Shorter vectors are recycled to match longer ones during arithmetic — watch for silent recycling bugs.
NAis R's proper way to represent missing values; usena.rm = TRUEin summary functions.