How to extract a subset from a Vector?

Extracting a subset of values from the front or rear of a vector is a common task and the base functions head() and tail() can do this. The infix functions in the extractors group mimic some of this behavior and add the ability to extract from - to, or, up -and-until, a specific value.

  • Vectors are basic objects in R and they can be subsetted using the [ operator. The [ operator can be used to extract multiple elements of a vector by passing the operator an integer sequence.

  • To extract (also known as indexing or subscripting) one or more values (more generally known as elements) from a vector we use the square bracket [ ] notation.