<- 20:25
sequence_example <- sequence_example[1]
first_element first_element
[1] 20
We can extract individual elements of a vector by using the square bracket notation:
<- 20:25
sequence_example <- sequence_example[1]
first_element first_element
[1] 20
To change a single element, use the bracket on the other side of the arrow:
1] <- 30
sequence_example[ sequence_example
[1] 30 21 22 23 24 25
Let’s define a new vector, x
:
<- c(5.4, 6.2, 7.1, 4.8, 7.5)
x x
[1] 5.4 6.2 7.1 4.8 7.5
So now that we’ve created a toy vector to play with, how do we get at its contents?
To extract elements of a vector we can give their corresponding index, starting from one:
1] x[
[1] 5.4
4] x[
[1] 4.8
The square brackets operator is a function. For vectors, it means “get me the nth element”.
We can ask for multiple elements at once by providing a vector of indices:
c(1, 3)] x[
[1] 5.4 7.1
Or “slices” of the vector using a sequential integer vector index:
1:4] x[
[1] 5.4 6.2 7.1 4.8
Recall that the :
operator creates a sequence of numbers from the left element to the right.
1:4
[1] 1 2 3 4
c(1, 2, 3, 4)
[1] 1 2 3 4
We can ask for the same element multiple times:
c(1, 1, 3)] x[
[1] 5.4 5.4 7.1
If we ask for an index beyond the length of the vector, R will return a missing value:
6] x[
[1] NA
This is a vector of length one containing an NA
, whose name is also NA
.
If we ask for the 0th element, we get an empty vector:
0] x[
numeric(0)
If we use a negative number as the index of a vector, R will return every element except for the one specified:
-2] x[
[1] 5.4 7.1 4.8 7.5
We can skip multiple elements:
c(-1, -5)] # or x[-c(1,5)] x[
[1] 6.2 7.1 4.8
To remove elements from a vector, we need to re-assign the variable to our result:
<- x[-4]
x x
[1] 5.4 6.2 7.1 7.5
With names, we can give meaning to elements. It is the first time that we do not only have the data, but also explaining information. It is metadata that can be stuck to the object like a label. In R, this is called an attribute. Some attributes enable us to do more with our object, for example, like here, accessing an element by a self-defined name.
Each element of a vector can be given a name:
<- c(pizzasubito = 5.64, pizzafresh = 6.60, callapizza = 4.50) pizza_price
To retrieve a specific named entry from a vector, we can use the square bracket notation:
"pizzasubito"] pizza_price[
pizzasubito
5.64
which is equivalent to extracting the first entry of the vector:
1] pizza_price[
pizzasubito
5.64
If you want to extract just the names of an object, use the names()
function:
names(pizza_price)
[1] "pizzasubito" "pizzafresh" "callapizza"
We have seen how to access and change single elements of a vector. The same is possible for names:
names(pizza_price)[3]
[1] "callapizza"
names(pizza_price)[3] <- "call-a-pizza"
pizza_price
pizzasubito pizzafresh call-a-pizza
5.64 6.60 4.50
Removing named elements is a little harder. If we try to remove one named element by negating the string, R complains (slightly obscurely) that it doesn’t know how to take the negative of a string:
<- c(a = 5.4, b = 6.2, c = 7.1, d = 4.8, e = 7.5) # we start again by naming a vector 'on the fly'
x -"a"] x[
Error in -"a": invalid argument to unary operator
We will discuss more about subsetting in the next lesson.