📏 R Vectors – Create, Access, and Manipulate 1D Data in R
🧲 Introduction – What Are Vectors in R?
Vectors are the most basic and essential data structure in R. A vector is a one-dimensional array that contains elements of the same data type—such as numbers, characters, or logical values.
Everything in R is built upon vectors. Whether you’re storing a list of names, test scores, or Boolean results, you’re working with vectors. Mastering vectors will make you more fluent in data analysis and R scripting.
🎯 In this guide, you’ll learn:
- How to create vectors using different functions
- Access and modify vector elements
- Perform vectorized operations and comparisons
- Use logical indexing and built-in vector functions
🧪 Creating Vectors in R
✅ Using c()
– Combine Function
numbers <- c(1, 2, 3, 4, 5)
names <- c("Alice", "Bob", "Charlie")
✅ Using :
– Sequence Operator
seq1 <- 1:5 # 1 2 3 4 5
✅ Using seq()
– Custom Sequence
seq(1, 10, by = 2) # 1 3 5 7 9
✅ Using rep()
– Repeat Elements
rep(3, times = 4) # 3 3 3 3
rep(c("A", "B"), 2) # "A" "B" "A" "B"
🧬 Types of Vectors
Type | Example |
---|---|
Numeric | c(1, 2, 3.5) |
Character | c("A", "B") |
Logical | c(TRUE, FALSE, TRUE) |
Integer | c(1L, 2L) |
Complex | c(1+2i, 2+3i) |
Use typeof()
or class()
to inspect vector type.
🧾 Accessing Elements in a Vector
🔢 By Index
x <- c(10, 20, 30, 40)
x[2] # 20
📐 By Range
x[2:4] # 20 30 40
🧼 Exclude Elements
x[-1] # Removes first element: 20 30 40
🔍 By Logical Index
x[x > 25] # 30 40
🔄 Modifying Vector Elements
x[2] <- 100 # Change second element
x # 10 100 30 40
You can also extend a vector:
x[5] <- 50 # Adds a fifth element
🔁 Vectorized Arithmetic Operations
R is vectorized—operations apply to each element automatically.
a <- c(1, 2, 3)
b <- c(10, 20, 30)
a + b # 11 22 33
a * 2 # 2 4 6
🔍 Vector Comparison & Logical Filtering
x <- c(5, 10, 15)
x > 8 # FALSE TRUE TRUE
x[x > 8] # 10 15
🧠 Useful Vector Functions
Function | Purpose | Example |
---|---|---|
length() | Count number of elements | length(x) |
sort() | Sort values in ascending order | sort(x) |
rev() | Reverse the vector | rev(x) |
unique() | Remove duplicates | unique(c(1,1,2)) |
sum() | Add all elements | sum(x) |
which() | Return indices of TRUE conditions | which(x > 10) |
any() | TRUE if any condition is TRUE | any(x > 20) |
all() | TRUE if all conditions are TRUE | all(x > 0) |
⚠️ Type Coercion in Mixed-Type Vectors
c(1, "two", TRUE) # All elements become character
R coerces to the most flexible type: logical < integer < numeric < character
.
📌 Summary – Recap & Next Steps
Vectors are at the core of data manipulation in R. They are flexible, fast, and power almost every structure in R programming.
🔍 Key Takeaways:
- Use
c()
,:
andseq()
to create vectors - Index elements with numbers, ranges, logical conditions
- Perform vectorized math and logical filtering
- Understand coercion rules for mixed types
- Use built-in functions like
sum()
,which()
, andunique()
⚙️ Real-World Relevance:
Vectors are crucial for filtering datasets, summarizing values, handling user input, and automating batch processing in statistical and machine learning projects.
❓ FAQs – Vectors in R
❓ What is the difference between a vector and a list in R?
✅ A vector is homogeneous (all elements same type), while a list can store mixed types (e.g., numbers and strings).
❓ Can I store a matrix in a vector?
✅ No. Matrices are separate 2D structures. But you can flatten a matrix into a vector using as.vector(matrix)
.
❓ How do I check if a variable is a vector?
✅ Use is.vector()
:
is.vector(c(1,2,3)) # TRUE
❓ What happens if I index beyond the length of a vector?
✅ You get NA
:
x <- c(1,2)
x[5] # NA
❓ How do I remove NA
values from a vector?
✅ Use na.omit()
or logical indexing:
x[!is.na(x)]
Share Now :