Psst! Check what courses we have in our offer, and get them all here to save $727. Act fast–this deal is available only 3/14–3/21.
What are vectors?
Vector operations
Indexing and filtering
14. Accessing multiple elements in the vector


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)]


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.