Vector Indexing
Vector indexing in R allows you to access or modify specific elements within a vector. R uses 1-based indexing, meaning that the index of the first element is 1. This section will cover the various ways to index and subset vectors, including positive and negative indexing, logical indexing, and more.
Positive Indexing
You can use positive integers to access elements of a vector. The index positions are specified by numbers, starting from 1.
Examples:
# Define a numeric vector vec <- c(10, 20, 30, 40, 50) # Access elements by positive indexing first_element <- vec[1] print(first_element) # Output: 10 # Access multiple elements subset_vec <- vec[c(2, 4)] print(subset_vec) # Output: 20 40
Explanation:
- vec[1] accesses the first element of the vector.
- vec[c(2, 4)] accesses the elements at positions 2 and 4.
Negative Indexing
Negative indexing allows you to exclude specific elements from the vector. The indices specified are removed from the vector.
Examples:
# Define a numeric vector vec <- c(10, 20, 30, 40, 50) # Exclude elements using negative indexing excluded_elements <- vec[-c(2, 4)] print(excluded_elements) # Output: 10 30 50
Explanation:
- vec[-c(2, 4)] returns the vector with the 2nd and 4th elements excluded.
Logical Indexing
Logical indexing allows you to select elements based on conditions. A logical vector of the same length as the original vector is used, with TRUE indicating elements to include and FALSE indicating elements to exclude.
Examples:
# Define a numeric vector vec <- c(10, 20, 30, 40, 50) # Create a logical vector logical_index <- c(TRUE, FALSE, TRUE, FALSE, TRUE) # Access elements based on logical indexing filtered_vec <- vec[logical_index] print(filtered_vec) # Output: 10 30 50
Explanation:
- logical_index specifies which elements to include based on the TRUE values.
Indexing with Names
Vectors in R can have names assigned to their elements, and you can index these vectors using these names.
Examples:
# Define a named vector named_vec <- c(a = 10, b = 20, c = 30, d = 40) # Access elements by name element_a <- named_vec["a"] print(element_a) # Output: 10 # Access multiple elements by name subset_named_vec <- named_vec[c("b", "d")] print(subset_named_vec) # Output: b 20 d 40
Explanation:
- named_vec[“a”] retrieves the value associated with the name “a”.
- named_vec[c(“b”, “d”)] retrieves values associated with the names “b” and “d”.
Indexing with Sequences
You can use sequences to index and access a range of elements in a vector. This is useful for selecting contiguous blocks of elements.
Examples:
# Define a numeric vector vec <- c(10, 20, 30, 40, 50) # Access elements using a sequence sequence_index <- vec[2:4] print(sequence_index) # Output: 20 30 40
Explanation:
- 2:4 creates a sequence from 2 to 4, which is used to access the 2nd through 4th elements of the vector.
Advanced Indexing with which()
The which() function is used to find the indices of elements that satisfy a condition. It returns the positions of the TRUE values in a logical vector.
Examples:
# Define a numeric vector vec <- c(10, 20, 30, 40, 50) # Find indices of elements greater than 25 indices <- which(vec > 25) print(indices) # Output: 3 4 5 # Use these indices to access the elements elements <- vec[indices] print(elements) # Output: 30 40 50
Explanation:
- which(vec > 25) returns the indices where the condition vec > 25 is TRUE.
Modifying Elements
You can modify elements of a vector by assigning new values to specific indices.
Examples:
# Define a numeric vector vec <- c(10, 20, 30, 40, 50) # Modify elements vec[3] <- 35 print(vec) # Output: 10 20 35 40 50 # Modify multiple elements vec[c(1, 5)] <- c(15, 55) print(vec) # Output: 15 20 35 40 55
Explanation:
- vec[3] <- 35 changes the value of the 3rd element to 35.
- vec[c(1, 5)] <- c(15, 55) changes the values of the 1st and 5th elements.
Handling Out-of-Bounds Indexes
When indexing with numbers outside the range of the vector, R returns an empty result or NA.
Examples:
# Define a numeric vector vec <- c(10, 20, 30) # Access out-of-bounds index out_of_bounds <- vec[5] print(out_of_bounds) # Output: NA # Access with a mix of in-bounds and out-of-bounds indexes mixed_index <- vec[c(1, 3, 5)] print(mixed_index) # Output: 10 30 NA
Explanation:
- Indexing with 5 returns NA because it is out of the bounds of the vector.
- A mix of valid and invalid indexes will return NA for the invalid indices.
Summary
Vector indexing in R is a versatile tool for accessing and manipulating elements in vectors. You can use positive indexing to access specific elements, negative indexing to exclude elements, and logical indexing to filter elements based on conditions. Named indexing allows you to use element names for access, while sequences and the which() function offer advanced indexing capabilities. Modifying elements and handling out-of-bounds indices are also important aspects of vector indexing.