• No products in the cart.

# 103.2.5.a Sorting of Data- An Example

##### Learn By Practice

In previous post we saw  Sorting of Data

In Autodataset we can find out the top or bottom rank in terms of the specifications of the cars. Like which car has the most mileage, and which car has the least mileage. Sorting helps us in getting such results. The below data set would help us in sorting the data into either ascending order or descending order.

1. AutoDataset
2. Sort the dataset based on length
3. Sort the dataset based on length descending

Solutions

1. Import AutoDataset
```>AutoDataset <- read.csv("C:\\Users\\venk\\Google Drive\\Training\\Datasets\\Automobile Data set\\AutoDataset.csv")
>str(AutoDataset)```
```## 'data.frame':    205 obs. of  26 variables:
##  \$ symboling        : int  3 3 1 2 2 2 1 1 1 0 ...
##  \$ normalized.losses: Factor w/ 52 levels "?","101","102",..: 1 1 1 29 29 1 27 1 27 1 ...
##  \$ make             : Factor w/ 22 levels "alfa-romero",..: 1 1 1 2 2 2 2 2 2 2 ...
##  \$ fuel.type        : Factor w/ 2 levels "diesel","gas": 2 2 2 2 2 2 2 2 2 2 ...
##  \$ aspiration       : Factor w/ 2 levels "std","turbo": 1 1 1 1 1 1 1 1 2 2 ...
##  \$ num.of.doors     : Factor w/ 3 levels "?","four","two": 3 3 3 2 2 3 2 2 2 3 ...
##  \$ body.style       : Factor w/ 5 levels "convertible",..: 1 1 3 4 4 4 4 5 4 3 ...
##  \$ drive.wheels     : Factor w/ 3 levels "4wd","fwd","rwd": 3 3 3 2 1 2 2 2 2 1 ...
##  \$ engine.location  : Factor w/ 2 levels "front","rear": 1 1 1 1 1 1 1 1 1 1 ...
##  \$ wheel.base       : num  88.6 88.6 94.5 99.8 99.4 ...
##  \$ length           : num  169 169 171 177 177 ...
##  \$ width            : num  64.1 64.1 65.5 66.2 66.4 66.3 71.4 71.4 71.4 67.9 ...
##  \$ height           : num  48.8 48.8 52.4 54.3 54.3 53.1 55.7 55.7 55.9 52 ...
##  \$ curb.weight      : int  2548 2548 2823 2337 2824 2507 2844 2954 3086 3053 ...
##  \$ engine.type      : Factor w/ 7 levels "dohc","dohcv",..: 1 1 6 4 4 4 4 4 4 4 ...
##  \$ num.of.cylinders : Factor w/ 7 levels "eight","five",..: 3 3 4 3 2 2 2 2 2 2 ...
##  \$ engine.size      : int  130 130 152 109 136 136 136 136 131 131 ...
##  \$ fuel.system      : Factor w/ 8 levels "1bbl","2bbl",..: 6 6 6 6 6 6 6 6 6 6 ...
##  \$ bore             : Factor w/ 39 levels "?","2.54","2.68",..: 25 25 3 15 15 15 15 15 12 12 ...
##  \$ stroke           : Factor w/ 37 levels "?","2.07","2.19",..: 6 6 29 26 26 26 26 26 26 26 ...
##  \$ compression.ratio: num  9 9 9 10 8 8.5 8.5 8.5 8.3 7 ...
##  \$ horsepower       : Factor w/ 60 levels "?","100","101",..: 7 7 22 4 10 6 6 6 17 25 ...
##  \$ peak.rpm         : Factor w/ 24 levels "?","4150","4200",..: 12 12 12 18 18 18 18 18 18 18 ...
##  \$ city.mpg         : int  21 21 19 24 18 19 19 19 17 16 ...
##  \$ highway.mpg      : int  27 27 26 30 22 25 25 25 20 22 ...
##  \$ price            : num  13495 16500 16500 13950 17450 ...```
1. Sort the dataset based on length
```>Autodataset_sort <- AutoDataset[order(AutoDataset\$length),]

```##    symboling normalized.losses      make fuel.type aspiration num.of.doors
## 19         2               121 chevrolet       gas        std          two
## 31         2               137     honda       gas        std          two
## 32         2               137     honda       gas        std          two
## 33         1               101     honda       gas        std          two
## 34         1               101     honda       gas        std          two
## 35         1               101     honda       gas        std          two
##    body.style drive.wheels engine.location wheel.base length width height
## 19  hatchback          fwd           front       88.4  141.1  60.3   53.2
## 31  hatchback          fwd           front       86.6  144.6  63.9   50.8
## 32  hatchback          fwd           front       86.6  144.6  63.9   50.8
## 33  hatchback          fwd           front       93.7  150.0  64.0   52.6
## 34  hatchback          fwd           front       93.7  150.0  64.0   52.6
## 35  hatchback          fwd           front       93.7  150.0  64.0   52.6
##    curb.weight engine.type num.of.cylinders engine.size fuel.system bore
## 19        1488           l            three          61        2bbl 2.91
## 31        1713         ohc             four          92        1bbl 2.91
## 32        1819         ohc             four          92        1bbl 2.91
## 33        1837         ohc             four          79        1bbl 2.91
## 34        1940         ohc             four          92        1bbl 2.91
## 35        1956         ohc             four          92        1bbl 2.91
##    stroke compression.ratio horsepower peak.rpm city.mpg highway.mpg price
## 19   3.03               9.5         48     5100       47          53  5151
## 31   3.41               9.6         58     4800       49          54  6479
## 32   3.41               9.2         76     6000       31          38  6855
## 33   3.07              10.1         60     5500       38          42  5399
## 34   3.41               9.2         76     6000       30          34  6529
## 35   3.41               9.2         76     6000       30          34  7129```
1. Sort the dataset based on length descending
```>Autodataset_sort2 <- AutoDataset[order(-AutoDataset\$length),]
```##    symboling normalized.losses          make fuel.type aspiration
## 74         0                 ? mercedes-benz       gas        std
## 71        -1                93 mercedes-benz    diesel      turbo
## 72        -1                 ? mercedes-benz       gas        std
## 48         0               145        jaguar       gas        std
## 49         0                 ?        jaguar       gas        std
## 75         1                 ? mercedes-benz       gas        std
##    num.of.doors body.style drive.wheels engine.location wheel.base length
## 74         four      sedan          rwd           front      120.9  208.1
## 71         four      sedan          rwd           front      115.6  202.6
## 72         four      sedan          rwd           front      115.6  202.6
## 48         four      sedan          rwd           front      113.0  199.6
## 49         four      sedan          rwd           front      113.0  199.6
## 75          two    hardtop          rwd           front      112.0  199.2
##    width height curb.weight engine.type num.of.cylinders engine.size
## 74  71.7   56.7        3900        ohcv            eight         308
## 71  71.7   56.3        3770         ohc             five         183
## 72  71.7   56.5        3740        ohcv            eight         234
## 48  69.6   52.8        4066        dohc              six         258
## 49  69.6   52.8        4066        dohc              six         258
## 75  72.0   55.4        3715        ohcv            eight         304
##    fuel.system bore stroke compression.ratio horsepower peak.rpm city.mpg
## 74        mpfi  3.8   3.35               8.0        184     4500       14
## 71         idi 3.58   3.64              21.5        123     4350       22
## 72        mpfi 3.46    3.1               8.3        155     4750       16
## 48        mpfi 3.63   4.17               8.1        176     4750       15
## 49        mpfi 3.63   4.17               8.1        176     4750       15
## 75        mpfi  3.8   3.35               8.0        184     4500       14
##    highway.mpg price
## 74          16 40960
## 71          25 31600
## 72          18 34184
## 48          19 32250
## 49          19 35550
## 75          16 45400

In the next post we will see Handling Duplicates.
```
20th June 2017