Subsetting is the selection or extraction of specific parts of larger data. We can subset on various kinds of data objects: vectors, lists, and data frames.
## Subsetting operators
There are three subsetting operators: `[`, `[[` and `$`.
`[[` is similar to `[`, except it can only return a single value and it allows you to pull elements out of a list.
`$` is a useful shorthand for `[[` combined with character subsetting.
You need `[[` when working with lists. This is because when `[` is applied to a list, it always returns a list; it never gives you the contents of the list.
The following are the examples of subsetting of various `R` objects:
**1. Vectors**
```r
x <-c(2.1,4.2,3.3,5.4)
x[c(3, 1)] # Subsetting using positive integers: return elements at the specified positions.
## [1] 3.3 2.1
x[-c(3, 1)] # Subsetting using positive integers: return elements at the specified positions.
## [1] 4.2 5.4
x[c(TRUE, TRUE, FALSE, FALSE)] # # Subsetting using logical vectors.