Creating Vectors
in R
Learn to build data structures using c(), the colon operator, and seq() to store and manipulate collections of values.
What Is a Vector?
A vector is R's most fundamental data structure. It is an ordered collection of elements that all share the same type: numeric, character, or logical. Almost everything in R is a vector.
Why Vectors Matter
R is a vectorized language: operations applied to a vector automatically act on every element. This eliminates the need for explicit loops and makes code both faster and cleaner.
Three Types of Vectors
The c() Function
The c() function stands for "combine". It is the primary tool for creating vectors by hand. You pass comma-separated values, and R returns a vector.
ages <- c(16, 17, 15, 18)
# Character vector
names <- c("Alice", "Bob", "Eve")
# Logical vector
passed <- c(TRUE, FALSE, TRUE, TRUE)
The Colon Operator :
The colon operator : generates a sequence of integers from a start value to an end value, incrementing by 1 (or decrementing by 1 if start > end).
x <- 1:5
# [1] 1 2 3 4 5
# Descending sequence
y <- 10:6
# [1] 10 9 8 7 6
The seq() Function
When you need a step size other than 1, or you want to specify exactly how many values to generate, use seq(). It accepts three key arguments.
seq(0, 1, by = 0.25)
# [1] 0.00 0.25 0.50 0.75 1.00
# Exact number of points with "length.out"
seq(1, 10, length.out = 4)
# [1] 1 4 7 10
seq() Anatomy
: vs seq()
| Colon : | seq() | |
|---|---|---|
| Step size | Always 1 | Any value via by |
| Non-integers | Steps by 1 (or -1) from the start value | Full control: 0.1, 0.5, etc. |
| Fixed count | Not supported | length.out |
| Syntax | a:b | seq(a, b, ...) |
| Speed | Marginally faster | More flexible |
How to Create a Vector
Example 1: Test Scores
A teacher records five quiz scores. Store them in a vector and compute the mean.
scores <- c(88, 92, 75, 95, 80)
# Compute the mean
mean(scores)
# [1] 86
# Check how many elements
length(scores)
# [1] 5
Example 2: Day Numbers
Create a vector of day numbers from 1 to 30 for a month-long experiment, then extract every 5th day.
days <- 1:30
# Every 5th day using seq()
checkpoints <- seq(5, 30, by = 5)
# [1] 5 10 15 20 25 30
Example 3: Precision Sampling
Generate exactly 6 evenly-spaced values between \(0\) and \(\pi\) for a trigonometry plot.
angles <- seq(0, pi, length.out = 6)
# [1] 0.0000 0.6283 1.2566 1.8850 2.5133 3.1416
# Use them in a function
sin(angles)
# [1] 0.0000 0.5878 0.9511 0.9511 0.5878 0.0000
Combining Vectors
You can use c() to concatenate existing vectors into one longer vector. This is how you build up data piece by piece.
group_a <- c(10, 20, 30)
group_b <- c(40, 50)
# Combine into one vector
all_data <- c(group_a, group_b)
# [1] 10 20 30 40 50
# Mix c() with sequences
mixed <- c(1:3, 10, 20:22)
# [1] 1 2 3 10 20 21 22
Vectorized Operations
Operations on vectors act element-by-element. This is the core power of R.
x <- c(1, 2, 3)
y <- c(10, 20, 30)
x + y # [1] 11 22 33
x * 2 # [1] 2 4 6
x > 1 # [1] FALSE TRUE TRUE
Common Mistakes
Question 1 of 5
What does the following R code produce?
c(3, 1, 4, 1, 5)
Question 2 of 5
Which expression generates the vector 5 6 7 8 9 10?
Question 3 of 5
What is the result of seq(0, 1, by = 0.5)?
Question 4 of 5
What does c(TRUE, 3, "hello") produce?
Question 5 of 5
How many elements does seq(2, 10, length.out = 5) produce?