Introduction
In this article, I am going to demonstrate how to create subsets of data using positive numbers for analysis of datasets so as to extract relevant data for creating a machine learning model. Extracting data from a dataset or creatinga subset of data is a part of data pre-processing techniques used in R to obtain clean and relevant data for accurate predictions to be made through a machine learning model.
For additional analysis of data in R, pre-processing of data is performed to create subsets of dataset. Several objects are available in R such as data frames, vectors, arrays and lists which can be used to create subsets of dataset and store the values of subset in them. There are different methods available to create subsets of vectors, arrays, data frames, and lists.
Performing analysis of data through pre-processing is one of the most important jobs in R. To create a subset of dataset in R several operators can be used. There are different types of operators for creating subsets of data. There are three kinds of operators which can be used to create different subsets which are as follows,
Dollar operator
We can create subsets of entire datasets by using the dollar operator. By mentioning dollar operator along with dataset name, we can select different variables of datasets at a time and create a subset of that variable alone as a vector. A vector object is formed when the dollar operator is used with a data frame.
Now we will discuss with some examples, on how to use dollar operator to create subset of dataset. We will be using quakes dataset to use different operators as follows,
- > data = quakes[1:50,]
- > data
- lat long depth mag stations
- 1 -20.42 181.62 562 4.8 41
- 2 -20.62 181.03 650 4.2 15
- 3 -26.00 184.10 42 5.4 43
- 4 -17.97 181.66 626 4.1 19
- 5 -20.42 181.96 649 4.0 11
- 6 -19.68 184.31 195 4.0 12
- 7 -11.70 166.10 82 4.8 43
- 8 -28.11 181.93 194 4.4 15
- 9 -28.74 181.74 211 4.7 35
- 10 -17.47 179.59 622 4.3 19
- 11 -21.44 180.69 583 4.4 13
- 12 -12.26 167.00 249 4.6 16
- 13 -18.54 182.11 554 4.4 19
- 14 -21.00 181.66 600 4.4 10
- 15 -20.70 169.92 139 6.1 94
- 16 -15.94 184.95 306 4.3 11
- 17 -13.64 165.96 50 6.0 83
- 18 -17.83 181.50 590 4.5 21
- 19 -23.50 179.78 570 4.4 13
- 20 -22.63 180.31 598 4.4 18
- 21 -20.84 181.16 576 4.5 17
- 22 -10.98 166.32 211 4.2 12
- 23 -23.30 180.16 512 4.4 18
- 24 -30.20 182.00 125 4.7 22
- 25 -19.66 180.28 431 5.4 57
- 26 -17.94 181.49 537 4.0 15
- 27 -14.72 167.51 155 4.6 18
- 28 -16.46 180.79 498 5.2 79
- 29 -20.97 181.47 582 4.5 25
- 30 -19.84 182.37 328 4.4 17
- 31 -22.58 179.24 553 4.6 21
- 32 -16.32 166.74 50 4.7 30
- 33 -15.55 185.05 292 4.8 42
- 34 -23.55 180.80 349 4.0 10
- 35 -16.30 186.00 48 4.5 10
- 36 -25.82 179.33 600 4.3 13
- 37 -18.73 169.23 206 4.5 17
- 38 -17.64 181.28 574 4.6 17
- 39 -17.66 181.40 585 4.1 17
- 40 -18.82 169.33 230 4.4 11
- 41 -37.37 176.78 263 4.7 34
- 42 -15.31 186.10 96 4.6 32
- 43 -24.97 179.82 511 4.4 23
- 44 -15.49 186.04 94 4.3 26
- 45 -19.23 169.41 246 4.6 27
- 46 -30.10 182.30 56 4.9 34
- 47 -26.40 181.70 329 4.5 24
- 48 -11.77 166.32 70 4.4 18
- 49 -24.12 180.08 493 4.3 21
- 50 -18.97 185.25 129 5.1 73
- >
Now we will use dollar operator with lat variable as follows,
- > ds = data$lat
- > ds
- [1] -20.42 -20.62 -26.00 -17.97 -20.42 -19.68 -11.70 -28.11 -28.74 -17.47 -21.44 -12.26 -18.54 -21.00 -20.70 -15.94 -13.64 -17.83 -23.50 -22.63 -20.84 -10.98 -23.30
- [24] -30.20 -19.66 -17.94 -14.72 -16.46 -20.97 -19.84 -22.58 -16.32 -15.55 -23.55 -16.30 -25.82 -18.73 -17.64 -17.66 -18.82 -37.37 -15.31 -24.97 -15.49 -19.23 -30.10
- [47] -26.40 -11.77 -24.12 -18.97
As we can see from the above output, using dollar operator with dataset and variable name a subset of quakes dataset is created. The subset is having lat variable and its observations. The subset is stored in a variable named ds.
- > df = data$long
- > df
- [1] 181.62 181.03 184.10 181.66 181.96 184.31 166.10 181.93 181.74 179.59 180.69 167.00 182.11 181.66 169.92 184.95 165.96 181.50 179.78 180.31 181.16 166.32 180.16
- [24] 182.00 180.28 181.49 167.51 180.79 181.47 182.37 179.24 166.74 185.05 180.80 186.00 179.33 169.23 181.28 181.40 169.33 176.78 186.10 179.82 186.04 169.41 182.30
- [47] 181.70 166.32 180.08 185.25
As we can see from the above output, using dollar operator with dataset and variable name a subset of quakes dataset is created. The subset is having long variable and its observations. The subset is stored in a variable named df.
- > da = data$dept
- > da
- [1] 562 650 42 626 649 195 82 194 211 622 583 249 554 600 139 306 50 590 570 598 576 211 512 125 431 537 155 498 582 328 553 50 292 349 48 600 206 574 585 230
- [41] 263 96 511 94 246 56 329 70 493 129
As we can see from the above output, using dollar operator with dataset and variable name a subset of quakes dataset is created. The subset is having dept variable and its observations. The subset is stored in a variable named da.
- > dn = data$mag
- >dn
- [1] 4.8 4.2 5.4 4.1 4.0 4.0 4.8 4.4 4.7 4.3 4.4 4.6 4.4 4.4 6.1 4.3 6.0 4.5 4.4 4.4 4.5 4.2 4.4 4.7 5.4 4.0 4.6 5.2 4.5 4.4 4.6 4.7 4.8 4.0 4.5 4.3 4.5 4.6 4.1 4.4
- [41] 4.7 4.6 4.4 4.3 4.6 4.9 4.5 4.4 4.3 5.1
As we can see from the above output, using dollar operator with dataset and variable name a subset of quakes dataset is created. The subset is having mag variable and its observations. The subset is stored in a variable named dn.
Double square brackets operator
The double square brackets operator can be used to create subsets of data containing either all observations of single variable of a dataset or just a single observation of a particular variable. For creating a subset using the double‐square‐brackets operator, we can use index position of the observations as well as name of the particular variable. We can use double square brackets operator with data frame.
- > data[['long']]
- [1] 181.62 181.03 184.10 181.66 181.96 184.31 166.10 181.93 181.74 179.59 180.69 167.00 182.11 181.66 169.92 184.95 165.96 181.50 179.78 180.31 181.16 166.32 180.16
- [24] 182.00 180.28 181.49 167.51 180.79 181.47 182.37 179.24 166.74 185.05 180.80 186.00 179.33 169.23 181.28 181.40 169.33 176.78 186.10 179.82 186.04 169.41 182.30
- [47] 181.70 166.32 180.08 185.25
As we can see above code snippet created a subset containing a single variable long. The argument is a variable name inside double square brackets operator.
- > data[[3]]
- [1] 562 650 42 626 649 195 82 194 211 622 583 249 554 600 139 306 50 590 570 598 576 211 512 125 431 537 155 498 582 328 553 50 292 349 48 600 206 574 585 230
- [41] 263 96 511 94 246 56 329 70 493 129
As we can see above code snippet created a subset containing a single variable dept. The argument is an index position of the variable named dept inside double square brackets operator.
- > data[[3]][2]
- [1] 650
- >
As we can see above code snippet created a subset containing a single observation of the variable dept. The arguments are an index positions of the rows and columns of that particular observation of the variable dept inside double square brackets operator.
Single square brackets operator
The single square brackets operator can be used to create subsets of data containing all observations of specified number of multiple variables of a dataset. Now we will discuss with some examples, on how to use single square brackets operator to create subset of dataset as follows,
- > data[1:8,]
- lat long depth mag stations
- 1 -20.42 181.62 562 4.8 41
- 2 -20.62 181.03 650 4.2 15
- 3 -26.00 184.10 42 5.4 43
- 4 -17.97 181.66 626 4.1 19
- 5 -20.42 181.96 649 4.0 11
- 6 -19.68 184.31 195 4.0 12
- 7 -11.70 166.10 82 4.8 43
- 8 -28.11 181.93 194 4.4 15
- >

Join the conversation! Your thoughts help the community grow.