๐ R Vectors: Your Train of Data
Imagine you have a toy train with many connected carriages. Each carriage holds one thingโa number, a word, or anything you want. Thatโs exactly what a vector is in R: a train of items, all connected and traveling together!
๐ฏ What is a Vector?
A vector is the most basic data structure in R. Think of it as a shopping bag that can hold multiple items of the same type.
- A bag of only apples ๐๐๐
- A bag of only numbers: 1, 2, 3, 4, 5
- A bag of only words: โhelloโ, โworldโ
The Golden Rule: All items in a vector must be the same type!
๐ ๏ธ Creating Vectors
The c() Function โ Your Magic Glue
The c() stands for โcombineโ or โconcatenateโ. Itโs like glue that sticks things together into one train!
# Numbers train
my_numbers <- c(10, 20, 30, 40, 50)
# Words train
my_words <- c("apple", "banana", "cherry")
# Logical train (TRUE/FALSE)
my_logic <- c(TRUE, FALSE, TRUE)
The : Operator โ Quick Sequences
Want numbers from 1 to 10? Donโt type them all!
# Creates: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
one_to_ten <- 1:10
# Going backwards works too!
ten_to_one <- 10:1
The seq() Function โ Custom Steps
Want to skip numbers? Use seq()!
# Every 2nd number from 0 to 10
by_twos <- seq(0, 10, by = 2)
# Result: 0, 2, 4, 6, 8, 10
# Exactly 5 numbers between 1 and 100
five_nums <- seq(1, 100, length.out = 5)
# Result: 1, 25.75, 50.5, 75.25, 100
The rep() Function โ Repeat After Me!
# Repeat 5 three times
rep(5, times = 3)
# Result: 5, 5, 5
# Repeat a pattern
rep(c(1, 2), times = 3)
# Result: 1, 2, 1, 2, 1, 2
๐ฏ Vector Indexing โ Finding Your Treasures
Indexing is like finding a specific carriage in your train. In R, the first carriage is number 1 (not 0 like some other languages!).
Basic Indexing with [ ]
fruits <- c("apple", "banana", "cherry",
"date", "elderberry")
# Get the 2nd fruit
fruits[2] # "banana"
# Get the 1st and 3rd fruit
fruits[c(1, 3)] # "apple", "cherry"
# Get fruits 2 through 4
fruits[2:4] # "banana", "cherry", "date"
Negative Indexing โ Exclude Items
# Everything EXCEPT the 1st fruit
fruits[-1]
# "banana", "cherry", "date", "elderberry"
# Exclude multiple items
fruits[-c(1, 5)]
# "banana", "cherry", "date"
Logical Indexing โ Smart Selection
scores <- c(85, 42, 91, 67, 78)
# Get scores above 70
scores[scores > 70]
# Result: 85, 91, 78
# Get scores between 50 and 90
scores[scores >= 50 & scores <= 90]
# Result: 85, 67, 78
โก Vector Operations โ Math on Steroids!
Element-wise Operations
When you do math on vectors, R does it to every element!
prices <- c(10, 20, 30)
# Add 5 to ALL prices
prices + 5
# Result: 15, 25, 35
# Double ALL prices
prices * 2
# Result: 20, 40, 60
Vector + Vector Operations
a <- c(1, 2, 3)
b <- c(10, 20, 30)
a + b # 11, 22, 33
a * b # 10, 40, 90
b - a # 9, 18, 27
b / a # 10, 10, 10
Recycling Rule ๐
When vectors have different lengths, R repeats the shorter one!
c(1, 2, 3, 4) + c(10, 20)
# R sees: c(1, 2, 3, 4) + c(10, 20, 10, 20)
# Result: 11, 22, 13, 24
โ ๏ธ Warning: Recycling can cause unexpected results if youโre not careful!
๐ Vectorized Computation โ Speed Secrets
Why Vectorization Matters
Instead of using slow loops, R can process entire vectors at once!
# SLOW way (don't do this!)
numbers <- 1:1000000
result <- numeric(1000000)
for(i in 1:1000000) {
result[i] <- numbers[i] * 2
}
# FAST way (do this!)
result <- numbers * 2
Built-in Vectorized Functions
nums <- c(4, 9, 16, 25)
sqrt(nums) # 2, 3, 4, 5
log(nums) # logarithm of each
abs(c(-1, 2, -3)) # 1, 2, 3
round(c(1.4, 2.6, 3.5)) # 1, 3, 4
Summary Functions
scores <- c(78, 92, 65, 88, 71)
sum(scores) # 394 (total)
mean(scores) # 78.8 (average)
max(scores) # 92 (highest)
min(scores) # 65 (lowest)
length(scores) # 5 (count)
๐ Vector Sorting and Ordering
sort() โ Arrange Items
messy <- c(5, 2, 8, 1, 9)
# Ascending (small to big)
sort(messy)
# Result: 1, 2, 5, 8, 9
# Descending (big to small)
sort(messy, decreasing = TRUE)
# Result: 9, 8, 5, 2, 1
order() โ Get Positions
order() tells you WHERE items would go, not the items themselves!
messy <- c(5, 2, 8, 1, 9)
order(messy)
# Result: 4, 2, 1, 3, 5
# Meaning: 4th item (1) is smallest,
# 2nd item (2) is next, etc.
# Use order to sort
messy[order(messy)]
# Same as sort(messy)!
rank() โ Whatโs My Place?
scores <- c(78, 92, 65, 88)
rank(scores)
# Result: 2, 4, 1, 3
# 78 is 2nd smallest
# 92 is 4th (largest)
# 65 is 1st (smallest)
# 88 is 3rd
rev() โ Reverse Order
letters <- c("a", "b", "c", "d")
rev(letters)
# Result: "d", "c", "b", "a"
๐ Vector Search Functions
which() โ Find Positions
scores <- c(78, 92, 65, 88, 71)
# Which positions have scores > 80?
which(scores > 80)
# Result: 2, 4 (positions of 92 and 88)
# Which is the maximum?
which.max(scores) # 2 (position of 92)
# Which is the minimum?
which.min(scores) # 3 (position of 65)
%in% โ Is It There?
fruits <- c("apple", "banana", "cherry")
# Single check
"apple" %in% fruits # TRUE
"mango" %in% fruits # FALSE
# Multiple checks
c("apple", "mango") %in% fruits
# Result: TRUE, FALSE
match() โ Find First Position
letters <- c("a", "b", "c", "b", "d")
# Where is "b" first found?
match("b", letters) # 2
# Multiple matches (first occurrence only)
match(c("b", "d"), letters) # 2, 5
any() and all() โ Quick Checks
scores <- c(78, 92, 65, 88, 71)
# Are ANY scores above 90?
any(scores > 90) # TRUE
# Are ALL scores above 60?
all(scores > 60) # TRUE
# Are ALL scores above 70?
all(scores > 70) # FALSE (65 fails)
๐จ Quick Reference Flow
graph LR A[Create Vector] --> B{What do you need?} B --> C[c func: Combine items] B --> D[colon: Quick sequence] B --> E[seq: Custom steps] B --> F[rep: Repeat items] G[Access Elements] --> H{How?} H --> I[Positive index: Get item] H --> J[Negative index: Exclude item] H --> K[Logical: Smart filter] L[Compute] --> M{Operation?} M --> N[Math: +, -, *, /] M --> O[Summary: sum, mean, max] M --> P[Transform: sqrt, log, abs]
๐ Key Takeaways
| Task | Function | Example |
|---|---|---|
| Create | c() |
c(1, 2, 3) |
| Sequence | : or seq() |
1:5 |
| Repeat | rep() |
rep(1, 3) |
| Access | [ ] |
x[2] |
| Sort | sort() |
sort(x) |
| Find positions | which() |
which(x > 5) |
| Check membership | %in% |
5 %in% x |
| Sum/Mean | sum(), mean() |
sum(x) |
๐ Congratulations! You now understand R vectors โ the building blocks of data analysis in R. Every data frame, every matrix, every analysis starts with vectors. Youโve just learned the foundation of R programming!