Vectors are fundamental data structures in R programming. They serve as the backbone for storing and manipulating collections of data elements of the same type. Understanding vectors is crucial for effective data analysis and manipulation in R.
An R vector is a one-dimensional array that can hold elements of a single data type, such as numeric, character, or logical values. Vectors are atomic, meaning they cannot contain elements of different types within the same vector.
There are several ways to create vectors in R:
The most common method is using the concatenate function, c():
numeric_vector <- c(1, 2, 3, 4, 5)
character_vector <- c("apple", "banana", "cherry")
logical_vector <- c(TRUE, FALSE, TRUE, TRUE)
For numeric sequences, you can use the colon operator or seq() function:
seq_vector <- 1:10
seq_by_2 <- seq(from = 1, to = 10, by = 2)
R provides powerful vectorized operations, allowing you to perform calculations on entire vectors efficiently:
v1 <- c(1, 2, 3, 4, 5)
v2 <- c(6, 7, 8, 9, 10)
sum_vector <- v1 + v2 # Element-wise addition
product_vector <- v1 * v2 # Element-wise multiplication
You can access and modify vector elements using indexing:
fruits <- c("apple", "banana", "cherry", "date")
fruits[2] # Returns "banana"
fruits[c(1, 3)] # Returns c("apple", "cherry")
fruits[-2] # Returns all elements except the second one
R provides numerous built-in functions for working with vectors:
length()
: Returns the number of elements in a vectorsum()
: Calculates the sum of all elements in a numeric vectormean()
: Computes the average of a numeric vectorsort()
: Arranges vector elements in ascending or descending orderVectorization in R offers significant performance advantages over loop-based operations. It allows for efficient and concise code, especially when dealing with large datasets. To learn more about this concept, check out the guide on R Vectorization.
Mastering R vectors is essential for efficient data manipulation and analysis. They form the foundation for more advanced data structures and are integral to many R Built-in Functions. As you progress in your R journey, you'll find vectors indispensable for tasks ranging from simple calculations to complex statistical analyses.