How to create a sequence of numbers in R
· 1 min read · Updated March 14, 2026 · beginner
r sequences vectors base-r
Creating sequences is fundamental in R. Here is how to do it.
With seq()
The seq() function is the main workhorse:
# From 1 to 10
seq(1, 10)
# [1] 1 2 3 4 5 6 7 8 9 10
# With a step of 2
seq(1, 10, by = 2)
# [1] 1 3 5 7 9
# With specific length
seq(1, 5, length.out = 10)
# [1] 1.000 1.444 1.889 2.333 2.778 3.222 3.667 4.111 4.556 5.000
With seq_len() and seq_along()
These are useful when you know the length but not the end:
# Sequence from 1 to n
n <- 5
seq_len(n)
# [1] 1 2 3 4 5
# Sequence along a vector (1 to length of vector)
x <- c("a", "b", "c")
seq_along(x)
# [1] 1 2 3
With rep()
Repeat values:
# Repeat a value
rep(1, times = 5)
# [1] 1 1 1 1 1
# Repeat a vector
rep(c(1, 2), times = 3)
# [1] 1 2 1 2 1 2
# Repeat each element
rep(c(1, 2), each = 3)
# [1] 1 1 1 2 2 2
Shortcuts: colon operator
For simple sequences, the colon is shortest:
1:10
# [1] 1 2 3 4 5 6 7 8 9 10