seq() for Custom Sequences
Control step size, length, and direction with seq().
Introduction to seq()
seq() is the flexible sequence generator in R. While : creates integer ranges with step 1, seq() lets you control the start, end, step size, and total length of any numeric sequence.
# Basic seq() usage
basic <- seq(1, 10)
cat('seq(1, 10):', basic, '
')
# With step size
by_two <- seq(0, 20, by = 2)
cat('seq(0, 20, by=2):', by_two, '
')seq(from, to, by) — Custom Step
The by argument sets the step size between consecutive values. It can be any positive or negative number, including decimals. This is useful for generating evenly spaced values at non-integer intervals.
# Decimal step
celsius <- seq(0, 100, by = 12.5)
cat('Every 12.5 degrees:', celsius, '
')
# Step of 0.1
fine_scale <- seq(1.0, 1.5, by = 0.1)
cat('Fine scale:', fine_scale, '
')
# Negative step (descending)
countdown <- seq(10, 0, by = -2)
cat('Countdown by 2:', countdown, '
')All lessons in this course
- The Colon Operator for Integer Ranges
- seq() for Custom Sequences
- rep() for Repeating Values
- Named Vectors and Named Sequences