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.
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
- >
As we can see from the above output single square brackets operator created a subset of quakes dataset containing all the variables and observations.
- > data[c(3,1,4)]
- depth lat mag
- 1 562 -20.42 4.8
- 2 650 -20.62 4.2
- 3 42 -26.00 5.4
- 4 626 -17.97 4.1
- 5 649 -20.42 4.0
- 6 195 -19.68 4.0
- 7 82 -11.70 4.8
- 8 194 -28.11 4.4
- 9 211 -28.74 4.7
- 10 622 -17.47 4.3
- 11 583 -21.44 4.4
- 12 249 -12.26 4.6
- 13 554 -18.54 4.4
- 14 600 -21.00 4.4
- 15 139 -20.70 6.1
- 16 306 -15.94 4.3
- 17 50 -13.64 6.0
- 18 590 -17.83 4.5
- 19 570 -23.50 4.4
- 20 598 -22.63 4.4
- 21 576 -20.84 4.5
- 22 211 -10.98 4.2
- 23 512 -23.30 4.4
- 24 125 -30.20 4.7
- 25 431 -19.66 5.4
- 26 537 -17.94 4.0
- 27 155 -14.72 4.6
- 28 498 -16.46 5.2
- 29 582 -20.97 4.5
- 30 328 -19.84 4.4
- 31 553 -22.58 4.6
- 32 50 -16.32 4.7
- 33 292 -15.55 4.8
- 34 349 -23.55 4.0
- 35 48 -16.30 4.5
- 36 600 -25.82 4.3
- 37 206 -18.73 4.5
- 38 574 -17.64 4.6
- 39 585 -17.66 4.1
- 40 230 -18.82 4.4
- 41 263 -37.37 4.7
- 42 96 -15.31 4.6
- 43 511 -24.97 4.4
- 44 94 -15.49 4.3
- 45 246 -19.23 4.6
The above code pulls out those variables and observations whose index positions are mentioned in the single square brackets operator and creates a subset of variables of 3, 1 and 4 index positions.
- > data[c(2,4,1)]
- long mag lat
- 1 181.62 4.8 -20.42
- 2 181.03 4.2 -20.62
- 3 184.10 5.4 -26.00
- 4 181.66 4.1 -17.97
- 5 181.96 4.0 -20.42
- 6 184.31 4.0 -19.68
- 7 166.10 4.8 -11.70
- 8 181.93 4.4 -28.11
- 9 181.74 4.7 -28.74
- 10 179.59 4.3 -17.47
- 11 180.69 4.4 -21.44
- 12 167.00 4.6 -12.26
- 13 182.11 4.4 -18.54
- 14 181.66 4.4 -21.00
- 15 169.92 6.1 -20.70
- 16 184.95 4.3 -15.94
- 17 165.96 6.0 -13.64
- 18 181.50 4.5 -17.83
- 19 179.78 4.4 -23.50
- 20 180.31 4.4 -22.63
- 21 181.16 4.5 -20.84
- 22 166.32 4.2 -10.98
- 23 180.16 4.4 -23.30
- 24 182.00 4.7 -30.20
- 25 180.28 5.4 -19.66
- 26 181.49 4.0 -17.94
- 27 167.51 4.6 -14.72
- 28 180.79 5.2 -16.46
- 29 181.47 4.5 -20.97
- 30 182.37 4.4 -19.84
- 31 179.24 4.6 -22.58
- 32 166.74 4.7 -16.32
- 33 185.05 4.8 -15.55
- 34 180.80 4.0 -23.55
- 35 186.00 4.5 -16.30
- 36 179.33 4.3 -25.82
- 37 169.23 4.5 -18.73
- 38 181.28 4.6 -17.64
- 39 181.40 4.1 -17.66
- 40 169.33 4.4 -18.82
- 41 176.78 4.7 -37.37
- 42 186.10 4.6 -15.31
- 43 179.82 4.4 -24.97
- 44 186.04 4.3 -15.49
- 45 169.41 4.6 -19.23
- 46 182.30 4.9 -30.10
- 47 181.70 4.5 -26.40
- 48 166.32 4.4 -11.77
Above code pull out those variables and observations whose index positions are mentioned in the single square brackets operator and creates a subset of variables of 2, 4 and 1 index positions.
The difference between the double square brackets operator and single square brackets is the indexing of number of variables. The [[ creates a subset of single variable and its observations and [ creates a subset of multiple variable and type of the subset is same as that of the dataset. For lists, one generally uses [[ to select any single element, whereas [ returns a list of the selected elements.
The [[ form allows only a single element to be selected using integer or character indices, whereas [ allows indexing by vectors. Note though that for a list or other recursive object, the index can be a vector and each element of the vector is applied in turn to the list, the selected component, the selected component of that component, and so on. The result is still a single element.
Now we will discuss how to use above mentioned operators to create the subsets of specified number of variables of a dataset. We will discuss methods to create subsets using positive numerical values of dataset containing data with all the variables and observations of the datasets.
Creating subsets using positive numerical values
The single square brackets operator creates a subset containing more than one variable. To create a subset of multiple variables, we can mention the required number of variables in the syntax of Single Square brackets operator to get a subset of multiple variables.
A subset using positive numerical values can be created using single square brackets operator preceded by dataset name. Such subsets contains only those variables and observations of a dataset whose index positions are mentioned inside square brackets. Using Single Square brackets operator preceded by dataset name we can mention the index numbers of required number of variables we want to insert in a resultant subset.
Now we will be using predefined dataset rock of type data frame containing four variables and 48 observations to create subsets using positive numerical values. We will be creating subsets using positive numerical values of several predefined datasets available in R as follows,
- > str(rock)
- 'data.frame': 48 obs. of 4 variables:
- $ area : int 4990 7002 7558 7352 7943 7979 9333 8209 8393 6425 ...
- $ peri : num 2792 3893 3931 3869 3949 ...
- $ shape: num 0.0903 0.1486 0.1833 0.1171 0.1224 ...
- $ perm : num 6.3 6.3 6.3 6.3 17.1 17.1 17.1 17.1 119 119 ...
- >
The subsets using positive numerical values for above rock dataset is as follows,
- > rock[c(4, 1, 2, 3)]
- perm area peri shape
- 1 6.3 4990 2791.900 0.0903296
- 2 6.3 7002 3892.600 0.1486220
- 3 6.3 7558 3930.660 0.1833120
- 4 6.3 7352 3869.320 0.1170630
- 5 17.1 7943 3948.540 0.1224170
- 6 17.1 7979 4010.150 0.1670450
- 7 17.1 9333 4345.750 0.1896510
- 8 17.1 8209 4344.750 0.1641270
- 9 119.0 8393 3682.040 0.2036540
- 10 119.0 6425 3098.650 0.1623940
- 11 119.0 9364 4480.050 0.1509440
- 12 119.0 8624 3986.240 0.1481410
- 13 82.4 10651 4036.540 0.2285950
- 14 82.4 8868 3518.040 0.2316230
- 15 82.4 9417 3999.370 0.1725670
- 16 82.4 8874 3629.070 0.1534810
- 17 58.6 10962 4608.660 0.2043140
- 18 58.6 10743 4787.620 0.2627270
- 19 58.6 11878 4864.220 0.2000710
- 20 58.6 9867 4479.410 0.1448100
- 21 142.0 7838 3428.740 0.1138520
- 22 142.0 11876 4353.140 0.2910290
- 23 142.0 12212 4697.650 0.2400770
- 24 142.0 8233 3518.440 0.1618650
- 25 740.0 6360 1977.390 0.2808870
- 26 740.0 4193 1379.350 0.1794550
- 27 740.0 7416 1916.240 0.1918020
- 28 740.0 5246 1585.420 0.1330830
- 29 890.0 6509 1851.210 0.2252140
- 30 890.0 4895 1239.660 0.3412730
- 31 890.0 6775 1728.140 0.3116460
- 32 890.0 7894 1461.060 0.2760160
- 33 950.0 5980 1426.760 0.1976530
- 34 950.0 5318 990.388 0.3266350
- 35 950.0 7392 1350.760 0.1541920
- 36 950.0 7894 1461.060 0.2760160
- 37 100.0 3469 1376.700 0.1769690
- 38 100.0 1468 476.322 0.4387120
- 39 100.0 3524 1189.460 0.1635860
- 40 100.0 5267 1644.960 0.2538320
- 41 1300.0 5048 941.543 0.3286410
- 42 1300.0 1016 308.642 0.2300810
- 43 1300.0 5605 1145.690 0.4641250
- 44 1300.0 8793 2280.490 0.4204770
- 45 580.0 3475 1174.110 0.2007440
- 46 580.0 1651 597.808 0.2626510
- 47 580.0 5514 1455.880 0.1824530
- 48 580.0 9718 1485.580 0.2004470
- >
The structure of mtcars dataset is as follows,
- > str(mtcars)
- 'data.frame': 32 obs. of 11 variables:
- $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
- $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
- $ disp: num 160 160 108 258 360 ...
- $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
- $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
- $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
- $ qsec: num 16.5 17 18.6 19.4 17 ...
- $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
- $ am : num 1 1 1 0 0 0 0 0 0 0 ...
- $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
- $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
- >
The subsets using positive numerical values of mtcars dataset is as follows,
- > mtcars[c(6, 5, 3, 8)]
- wt drat disp vs
- Mazda RX4 2.620 3.90 160.0 0
- Mazda RX4 Wag 2.875 3.90 160.0 0
- Datsun 710 2.320 3.85 108.0 1
- Hornet 4 Drive 3.215 3.08 258.0 1
- Hornet Sportabout 3.440 3.15 360.0 0
- Valiant 3.460 2.76 225.0 1
- Duster 360 3.570 3.21 360.0 0
- Merc 240D 3.190 3.69 146.7 1
- Merc 230 3.150 3.92 140.8 1
- Merc 280 3.440 3.92 167.6 1
- Merc 280C 3.440 3.92 167.6 1
- Merc 450SE 4.070 3.07 275.8 0
- Merc 450SL 3.730 3.07 275.8 0
- Merc 450SLC 3.780 3.07 275.8 0
- Cadillac Fleetwood 5.250 2.93 472.0 0
- Lincoln Continental 5.424 3.00 460.0 0
- Chrysler Imperial 5.345 3.23 440.0 0
- Fiat 128 2.200 4.08 78.7 1
- Honda Civic 1.615 4.93 75.7 1
- Toyota Corolla 1.835 4.22 71.1 1
- Toyota Corona 2.465 3.70 120.1 1
- Dodge Challenger 3.520 2.76 318.0 0
- AMC Javelin 3.435 3.15 304.0 0
- Camaro Z28 3.840 3.73 350.0 0
- Pontiac Firebird 3.845 3.08 400.0 0
- Fiat X1-9 1.935 4.08 79.0 1
- Porsche 914-2 2.140 4.43 120.3 0
- Lotus Europa 1.513 3.77 95.1 1
- Ford Pantera L 3.170 4.22 351.0 0
- Ferrari Dino 2.770 3.62 145.0 0
- Maserati Bora 3.570 3.54 301.0 0
- Volvo 142E 2.780 4.11 121.0 1
- >
Summary
In this article, I demonstrated how to create subsets of dataset using positive numerical values for analysis of dataset so as to extract relevant data. Different kinds of operators and datasets are used to create subsets of dataset using positive numerical values. Proper coding snippets along with outputs are also provided.