Introduction
In this article, I am going to demonstrate how to create subsets of data using negative numerical values for analysis of datasets so as to extract relevant data for creating a machine learning model. Negative numerical values creates subsets of data containing all the observations and excluding those values which are mentioned inside the square brackets along with negative sign.
Extracting data from datasets or creating subset of data is a part of data pre-processing technique used in R to obtain clean and relevant 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 datasets 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 which are as follows.
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,
We can create subsets of entire dataset by using the dollar operator. By mentioning dollar operator along with dataset name, we can select different variables of dataset 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 creating subsets of dataset using negative numerical values. We will be using quakes dataset to use different operators as follows,
- > data = quakes[-(30:990),]
- > 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
- 991 -20.73 181.42 575 4.3 18
- 992 -15.45 181.42 409 4.3 27
- 993 -20.05 183.86 243 4.9 65
- 994 -17.95 181.37 642 4.0 17
- 995 -17.70 188.10 45 4.2 10
- 996 -25.93 179.54 470 4.4 22
- 997 -12.28 167.06 248 4.7 35
- 998 -20.13 184.20 244 4.5 34
- 999 -17.40 187.80 40 4.5 14
- 1000 -21.59 170.56 165 6.0 119
- >
As we can see from the code above, a subset of dataset quake has been created, which contains all the variables and include only those observations which are not mentioned inside parenthesis along with negative sign.
- > data = quakes[-(40:980),-(2:4)]
- > data
- lat stations
- 1 -20.42 41
- 2 -20.62 15
- 3 -26.00 43
- 4 -17.97 19
- 5 -20.42 11
- 6 -19.68 12
- 7 -11.70 43
- 8 -28.11 15
- 9 -28.74 35
- 10 -17.47 19
- 11 -21.44 13
- 12 -12.26 16
- 13 -18.54 19
- 14 -21.00 10
- 15 -20.70 94
- 16 -15.94 11
- 17 -13.64 83
- 18 -17.83 21
- 19 -23.50 13
- 20 -22.63 18
- 21 -20.84 17
- 22 -10.98 12
- 23 -23.30 18
- 24 -30.20 22
- 25 -19.66 57
- 26 -17.94 15
- 27 -14.72 18
- 28 -16.46 79
- 29 -20.97 25
- 30 -19.84 17
- 31 -22.58 21
- 32 -16.32 30
- 33 -15.55 42
- 34 -23.55 10
- 35 -16.30 10
- 36 -25.82 13
- 37 -18.73 17
- 38 -17.64 17
- 39 -17.66 17
- 981 -20.82 67
- 982 -22.95 21
- 983 -28.22 49
- 984 -27.99 22
- 985 -15.54 17
- 986 -12.37 16
- 987 -22.33 51
- 988 -22.70 27
- 989 -17.86 12
- 990 -16.00 33
- 991 -20.73 18
- 992 -15.45 27
- 993 -20.05 65
- 994 -17.95 17
- 995 -17.70 10
- 996 -25.93 22
- 997 -12.28 35
- 998 -20.13 34
- 999 -17.40 14
- 1000 -21.59 119
- >
As we can see from the code above, a subset of dataset quake has been created, which contains all the variables and observations but exclude those variables and observations which are mentioned inside parenthesis along with negative sign.
Now we will use dollar operator with lat variable as follows,
- > ds = data$lat[-(10:20)]
- > ds
- [1] -20.42 -20.62 -26.00 -17.97 -20.42 -19.68 -11.70 -28.11 -28.74 -20.84 -10.98 -23.30 -30.20 -19.66 -17.94 -14.72 -16.46 -20.97 -19.84 -22.58 -16.32 -15.55 -23.55
- [24] -16.30 -25.82 -18.73 -17.64 -17.66 -20.82 -22.95 -28.22 -27.99 -15.54 -12.37 -22.33 -22.70 -17.86 -16.00 -20.73 -15.45 -20.05 -17.95 -17.70 -25.93 -12.28 -20.13
- [47] -17.40 -21.59
- >
As we can see from the above output, using dollar operator with dataset and variable name a subset of quakes dataset is created. Here we are creating subsets using negative numerical values. The subset is having lat variable and its observations. The subset is stored in a variable named ds. The subset extracts all the elements but exclude those elements whose index positions are mentioned inside parenthesis along with negative sign.
- > df = data$stations[-(11:19)]
- > df
- [1] 41 15 43 19 11 12 43 15 35 19 18 17 12 18 22 57 15 18 79 25 17 21 30 42 10 10 13 17 17 17 67 21 49 22 17 16 51 27 12 33
- [41] 18 27 65 17 10 22 35 34 14 119
- >
As we can see from the above output, using dollar operator with dataset and variable name a subset of quakes dataset is created. Here we are creating subsets using negative numerical values. The subset is having stations variable and its observations. The subset is stored in a variable named df. The subset extracts all the elements but exclude those elements whose index positions are mentioned from 11 to 19 inside parenthesis along with negative sign.
- > dn = data$dept[-(5:10)]
- > dn
- [1] 562 650 42 626 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 577 42 75 71 60 291 125
- [41] 69 614 108 575 409 243 642 45 470 248 244 40 165
- >
As we can see from the above output, using dollar operator with dataset and variable name a subset of quakes dataset is created. Here we are creating subsets using negative numerical values. The subset is having dept variable and its observations. The subset is stored in a variable named dn. The subset extracts all the elements but exclude those elements whose index positions are mentioned from 5 to 10 inside parenthesis along with negative sign.
- > da = data$mag[-(5:10)]
- > da
- [1] 4.8 4.2 5.4 4.1 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 5.0 4.7 4.9 4.3 4.5 4.2 5.2
- [41] 4.8 4.0 4.7 4.3 4.3 4.9 4.0 4.2 4.4 4.7 4.5 4.5 6.0
- >
As we can see from the above output, using dollar operator with dataset and variable name a subset of quakes dataset is created. Here we are creating subsets using negative numerical values. The subset is having mag variable and its observations. The subset is stored in a variable named da. The subset extracts all the elements but exclude those elements whose index positions are mentioned from 5 to 10 inside parenthesis along with negative sign.
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.

Join the conversation! Your thoughts help the community grow.