(y <- letters[1:10]) [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
We often want to select part of vector, rather than working with the whole thing. For example, we may want to overwrite some subset of the vector’s values, or extract part of the vector to use in some other calculation. On this page, we’ll cover methods for accessing and modifying subsets of a vector’s elements.
First, we’ll create a character vector, y, to use in the following examples.
The letters object is provided by Base R: we don’t need to create it. letters is a length-26 character vector that contains all the lower-case letters in the English alphabet.
We can access specific elements in a vector using the square bracket selection operator, []. To select specific elements, provide an integer vector defining the positions of the desired elements.
[1] "c"
[1] "b" "c" "d" "e"
[1] "d" "f" "h" "g"
Predict: What will the following code print?
This command produces the same result as the previous command (i.e., d, f, g, h). Now, we’re just using a different syntax to define the indexing vector: 4, 6, 7, 8.
When selecting vector elements, we must use a single vector to index the target elements.
Specifying multiple arguments inside [] is reserved for multi-dimensional structures like matrices or data frames and will trigger an error with vectors. If this distinction is confusing, don’t worry. We’ll come back this topic when we discuss matrices in the next tutorial.
You can use the head() or tail() functions to select the first or last n elements in a vector.
When you supply a negative number for the n argument in head() or tail(), the functions do something special. Can you figure out what’s happening?
Do: Run the following code, and use the results to explain what happens when n is negative.
When you specify a negative value for n, head() still select elements from beginning of the vector, but it defines the subset of selected elements by excluding the specified number of elements from the ending of the vector (rather than including that many elements from the beginning of the vector). Naturally, tail() does the same thing for elements at the end of the vector.
So, in this case:
head(y, n = -3) excludes the last 3 entries in y and prints the first 7 entries.tail(y, n = -3) excludes the first 3 entries in y and prints the last 7 entries.Above, we used the selection operator, [], to extract vector elements, but you shouldn’t really think about [] as a “subsetting operator”. I prefer to think about the selection operator as designating some part of a vector for further processing. In the above examples, the further processing was simply printing the selected elements to the R console, but we can also use the [] operator to modify the designated vector elements.
We can overwrite vector elements with new values by selecting the target elements using the [] operator and assigning new values with the ordinary assignment operator, <-.
[1] "bob" "b" "c" "d" "e" "f" "g" "h" "i" "j"
# Replace the fourth and sixth elements of y with "foo" and "bar", respectively
y[c(4, 6)] <- c("foo", "bar")
y [1] "bob" "b" "c" "foo" "e" "bar" "g" "h" "i" "j"
Predict: What will the following code print?
x that comprises the positive integers between 10 and 21 (inclusive).x and overwrites those values with 0.
So, we get the following result.
[1] 10 11 0 13 14 0 16
Overwriting vector elements uses ordinary recycling to harmonize the lengths of the selected set of elements and the length of the replacement vector.
[1] "bob" "b" "c" "foo" "e" "bar" "g" "alice" "alice"
[10] "alice"
Warning in y[1:3] <- c("hello", "world"): number of items to replace is not a
multiple of replacement length
[1] "hello" "world" "hello" "foo" "e" "bar" "g" "alice" "alice"
[10] "alice"
If the replacement vector is longer than the target vector, any extra elements in the replacement vector are dropped with a warning
Warning in y[1:4] <- c("It", "was", "the", "best", "of", "times"): number of
items to replace is not a multiple of replacement length
[1] "It" "was" "the" "best" "e" "bar" "g" "alice" "alice"
[10] "alice"
myVec that contains all the whole numbers from 1 to 11 (inclusive).myVec with 0.y is a length-five numeric vector, what would happen if you tried to run y[2, 3]?You’re ready to move on to Matrices if you can:
c(), :, seq(), and rep().[].Take the Knowledge Quiz to check your learning.