Creating subsets of vectors

Creating subsets of data is one of the fundamental operations in data analysis. In this section, we will cover the two basic ways to create subsets of a vector. The first way involves numeric vectors, which specify the requested indices to be included in the subset. The second way involves using logical vectors, which specify for each element whether we would like to keep it or not.

Subsetting with numeric vectors of indices

Subsetting using numeric vectors of indices is done using the square brackets operator [, by providing the vector of indices within the square brackets. For example, we can select a single element of a vector by putting the value of the required index within brackets, as follows:

> x = c(5,6,1,2,3,7) ...

Get Learning R for Geospatial Analysis now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.