What are vectors in R?
In R, a vector is the simplest and most important data structure. It is a collection of elements that are all of the same type, such as numbers, text, or logical values.
A vector is like a row of values stored in a single variable.
How to create a vector
You create vectors in R using the c() function (combine function).
numbers <- c(1, 2, 3, 4, 5)
This creates a numeric vector with five elements.
Types of vectors
- 🔢 Numeric vectors — numbers (e.g. 1, 2, 3)
- 🔤 Character vectors — text (e.g. "apple", "banana")
- ✔️ Logical vectors — TRUE / FALSE values
Examples
# Numeric vector nums <- c(10, 20, 30) # Character vector names <- c("Asha", "Rahul", "Meena") # Logical vector flags <- c(TRUE, FALSE, TRUE)
How vectors work
One powerful feature of R is that operations are applied to every element of a vector automatically.
nums <- c(1, 2, 3)
nums * 2
Output:
2 4 6
Indexing vectors
You can access elements using square brackets [].
nums <- c(10, 20, 30) nums[1] # 10
Why vectors matter
- 📊 Core building block of R programming
- ⚙️ Used in all data analysis tasks
- 🚀 Enables fast mathematical operations
- 📦 Basis for matrices, lists, and data frames
Almost everything in R — from data frames to statistical models — is built on vectors.
Summary
Vectors in R are basic structures that store elements of the same type. They allow fast operations and form the foundation of data analysis in R.
In short: A vector is a simple, powerful container for data in R.