Welcome to The Coding College! In this tutorial, we’ll explore one of the most fundamental data structures in R: vectors. Understanding vectors is key to mastering data manipulation and analysis in R, as they form the backbone of many operations in this programming language.
By the end of this guide, you’ll learn:
- What vectors are.
- How to create and manipulate vectors.
- Common operations you can perform with vectors.
What Are Vectors in R?
In R, a vector is a collection of elements of the same data type, such as numeric, character, or logical. Vectors are the simplest and most commonly used data structures in R and are essential for storing and processing data.
Types of Vectors in R:
- Numeric Vector: Contains numeric values.
- Character Vector: Contains text strings.
- Logical Vector: Contains
TRUE
orFALSE
. - Integer Vector: Contains integers.
- Complex Vector: Contains complex numbers.
How to Create Vectors in R
You can create vectors using the c()
function (short for combine).
Example: Creating Vectors
# Numeric vector
numeric_vec <- c(1, 2, 3, 4, 5)
# Character vector
char_vec <- c("apple", "banana", "cherry")
# Logical vector
logical_vec <- c(TRUE, FALSE, TRUE)
# Integer vector
int_vec <- c(1L, 2L, 3L)
# Complex vector
complex_vec <- c(1+2i, 3-4i)
# Print vectors
print(numeric_vec)
print(char_vec)
print(logical_vec)
Vector Properties
Vectors in R have several useful properties:
1. Length of a Vector
Use the length()
function to find the number of elements in a vector.
numeric_vec <- c(10, 20, 30, 40)
length(numeric_vec)
# Output: 4
2. Data Type of a Vector
Check the data type using the typeof()
or class()
function.
typeof(numeric_vec)
# Output: "double"
3. Naming Vector Elements
You can assign names to elements in a vector using the names()
function.
named_vec <- c(10, 20, 30)
names(named_vec) <- c("A", "B", "C")
print(named_vec)
# Output:
# A B C
# 10 20 30
Accessing Elements in Vectors
You can access individual elements or subsets of a vector using indexing.
1. Accessing by Index
numeric_vec <- c(10, 20, 30, 40)
numeric_vec[2]
# Output: 20
2. Accessing by Name
named_vec <- c(10, 20, 30)
names(named_vec) <- c("A", "B", "C")
named_vec["B"]
# Output: 20
3. Accessing Multiple Elements
numeric_vec[c(1, 3)]
# Output: 10 30
Modifying Vectors
You can modify elements of a vector by assigning new values.
Example: Modifying Vector Elements
numeric_vec <- c(10, 20, 30, 40)
# Modify the second element
numeric_vec[2] <- 25
# Add a new element
numeric_vec[5] <- 50
print(numeric_vec)
# Output: 10 25 30 40 50
Common Vector Operations
1. Arithmetic Operations
You can perform arithmetic operations on vectors, which are applied element-wise.
vec1 <- c(1, 2, 3)
vec2 <- c(4, 5, 6)
# Addition
vec1 + vec2
# Output: 5 7 9
# Multiplication
vec1 * vec2
# Output: 4 10 18
2. Logical Operations
Logical comparisons are also applied element-wise.
vec <- c(10, 20, 30)
# Greater than 15
vec > 15
# Output: FALSE TRUE TRUE
3. Vector Functions
R provides built-in functions to process vectors.
Sum:
sum(vec)
# Output: 60
Mean:
mean(vec)
# Output: 20
Sorting:
sort(vec, decreasing = TRUE)
# Output: 30 20 10
Combining Vectors
You can combine two or more vectors using the c()
function.
Example: Combining Vectors
vec1 <- c(1, 2, 3)
vec2 <- c(4, 5, 6)
combined_vec <- c(vec1, vec2)
print(combined_vec)
# Output: 1 2 3 4 5 6
Subsetting Vectors
Subsetting allows you to extract specific elements based on conditions.
Example: Subsetting a Vector
vec <- c(10, 20, 30, 40)
# Subset elements greater than 20
subset_vec <- vec[vec > 20]
print(subset_vec)
# Output: 30 40
Recycling Rule in Vectors
If you perform operations on vectors of different lengths, R recycles the shorter vector to match the length of the longer one.
Example:
vec1 <- c(1, 2, 3)
vec2 <- c(4, 5)
result <- vec1 + vec2
print(result)
# Output: 5 7 7 (vec2 is recycled)
Vector Functions Cheat Sheet
Here’s a quick list of commonly used functions for vectors in R:
Function | Purpose |
---|---|
length() | Get the length of a vector |
sum() | Calculate the sum of vector elements |
mean() | Calculate the mean of a vector |
sort() | Sort vector elements |
min() / max() | Find the minimum/maximum value |
unique() | Find unique elements in a vector |
which() | Find the indices of elements meeting a condition |
Best Practices for Working with Vectors
- Keep Data Types Consistent:
- R automatically converts mixed types to the most general type.
vec <- c(1, "apple", TRUE)
print(vec)
# Output: "1" "apple" "TRUE" (all elements converted to characters)
- Use Named Vectors for Clarity:
- Assign meaningful names to vector elements to improve code readability.
- Avoid Overwriting Built-In Functions:
- Don’t name your vectors after existing R functions like
mean
,sum
, orlength
.
- Don’t name your vectors after existing R functions like
FAQs About R Vectors
1. Can a vector contain multiple data types?
No, all elements in an R vector must be of the same data type. If you mix types, R will coerce them to the most general type (e.g., numeric -> character).
2. What’s the difference between a vector and a list in R?
Vectors can only store elements of the same type, while lists can store elements of different types.
3. How do I remove elements from a vector?
You can remove elements by setting them to NULL
or subsetting the vector.
Example:
vec <- c(1, 2, 3, 4)
vec <- vec[-2]
print(vec)
# Output: 1 3 4
Conclusion
Vectors are the cornerstone of data handling in R. Whether you’re performing simple arithmetic or complex data analysis, mastering vectors will help you write efficient and clean R code.