01 / 21

Creating Vectors
in R

Learn to build data structures using c(), the colon operator, and seq() to store and manipulate collections of values.

R Programming

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.

10 20 30 40 50 [1] [2] [3] [4] [5] my_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.

Store Data
Hold a column of measurements, a list of names, or a series of TRUE/FALSE values.
Vectorized Ops
Arithmetic, comparisons, and functions apply to all elements at once: no loops needed.
Build Blocks
Data frames, matrices, and lists are all built from vectors under the hood.

Three Types of Vectors

Numeric
Numbers: integers and doubles. Example: c(1, 2.5, 3)
Character
Text strings wrapped in quotes. Example: c("a", "b", "c")
Logical
Boolean values: TRUE or FALSE. Example: c(TRUE, FALSE, TRUE)

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.

# Numeric 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).

# Ascending sequence
x <- 1:5
# [1] 1 2 3 4 5

# Descending sequence
y <- 10:6
# [1] 10 9 8 7 6
1 2 3 4 5 1:5

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.

# Custom step size with "by"
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

seq(from, to, by) from start value to end value by step size OR replace "by" with: length.out = n

: 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

1
Identify the values. Are they arbitrary (hand-picked), or do they follow a pattern (sequence)?
2
Choose the right tool. Use c() for hand-picked values, : for integer sequences, seq() for custom sequences.
3
Set parameters. For seq(), decide between by (step) and length.out (count).
4
Assign to a variable. Store the result with <- for later use.
5
Verify. Print the variable or use length() and class() to confirm the result.

Example 1: Test Scores

A teacher records five quiz scores. Store them in a vector and compute the mean.

# Store the scores
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.

# All 30 days using the colon operator
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.

# 6 points from 0 to pi
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.

# Two separate groups
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.

# Create two vectors
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
x: 1 2 3 + y: 10 20 30 = result: 11 22 33

Common Mistakes

✕ Forgetting c()
Writing x <- 1, 2, 3 instead of x <- c(1, 2, 3). Without c(), R throws an error.
✕ Mixing Types
Using c(1, "two") silently converts 1 to "1". Check with class() to catch coercion.
✕ Operator Precedence
1:3 + 1 gives c(2, 3, 4), not 1:4. The colon binds tighter than +. Use 1:(3+1) if you mean 1:4.
✕ by and length.out
Using both by and length.out in seq() at once causes an error. Pick one or the other.

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?