Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
What are vectors?
Vector operations
Indexing and filtering
14. Accessing multiple elements in the vector
Summary

Instruction

Great! What if you want to access multiple elements at once? Instead of a single number, we can use a numeric vector inside brackets, to specify multiple index positions. Suppose we would like to look at the first four ages that are stored in our ages vector.

If we type

ages[c(1, 2, 3, 4)]
R will print the first, second, third, and fourth values. Likewise, if we'd just like to get the second and fourth values, we'd execute the code:

ages[c(2, 4)]

Exercise

The first four entries in the vector salaries correspond to employees in Department 1. The next three entries that follow in the vector belong to employees in Department 2.

Write a line of code that will retrieve the salaries of employees in Department 2 from the vector.