Spring Deals - hours only!Up to 70% off on all courses and bundles.-Close
What are vectors?
Vector operations
Indexing and filtering
13. Accessing single elements
Summary

Instruction

Nicely done! What if we would like to or use only one specific element of a vector? Let's take a look at how this can be done.

The first element of the vector a <- c(8, 9, 10) can be accessed like this:

a[1]

Here, a is a vector. Inside the square brackets, we specify the index position (1) of the element we would like to retrieve. R will respond by printing the value of the first element in vector a. Likewise, we can access the second element by using an index of 2, like so:

a[2]

Note that some programming languages use 0 as the index position of the first element in a vector. However, R uses the index position 1 to reference the first element of a vector.

Exercise

Jamie was the first to arrive at Versico. Thus, his salary is the first element in the vector named salaries. Write a line of code that will retrieve Jamie's salary.