Import Library
library(plyr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Read The file Using read_csv function
mtcars_Data <- read.csv("C:\\Users\\HP\\Documents\\R\\Project_2\\Rscripts\\mtcars.csv")
view the top rows
head(mtcars_Data)
## model mpg cyl disp hp drat wt qsec vs am gear carb
## 1 Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## 2 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## 3 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## 4 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## 5 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## 6 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
attach(mtcars_Data)# For Without Giving $ sign to accessing the columns
Question and Answer Regarding this dataset
1. get the unique count of cars?
- using ‘n_distinct’ funtion we make the count
mtcars_Data %>%
summarise(unique_count_cars = n_distinct(model))
## unique_count_cars
## 1 32
Insight: Their are 32 no of unique car in the dataset
2. get mean of weight by cylinder?
mtcars_Data %>% group_by(cyl) %>%
summarise(mean_of_weight = mean(wt))
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 3 x 2
## cyl mean_of_weight
## <int> <dbl>
## 1 4 2.29
## 2 6 3.12
## 3 8 4.00
Insight: The above table denotes the reasult
3. get the no of cars by gear?
mtcars_Data %>% group_by(gear) %>%
summarise(Count_Of_Cars = n_distinct(model))
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 3 x 2
## gear Count_Of_Cars
## <int> <int>
## 1 3 15
## 2 4 12
## 3 5 5
Insight: The above table denotes the reasult
4. get mean mpg by gear and carb and cyl?
mtcars_Data %>% group_by(gear) %>%
summarise(mean_mpg_by_gear = mean(mpg))
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 3 x 2
## gear mean_mpg_by_gear
## <int> <dbl>
## 1 3 16.1
## 2 4 24.5
## 3 5 21.4
Insight: The above table denotes the reasult ‘mean_mpg_by_gear’
mtcars_Data %>% group_by(cyl) %>%
summarise(mean_carb_by_cyl = mean(carb))
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 3 x 2
## cyl mean_carb_by_cyl
## <int> <dbl>
## 1 4 1.55
## 2 6 3.43
## 3 8 3.5
Insight: The above table denotes the reasult’mean_carb_by_cyl’
5. which car has the max hp ?
mtcars_Data %>%
filter(hp == max(hp)) %>%
select(model, hp)
## model hp
## 1 Maserati Bora 335
Insight: Maserati Bora has Highest HP-‘335’
6. which car has the minimum displacement ?
mtcars_Data %>%
filter(disp == min(disp)) %>%
select(model, disp)
## model disp
## 1 Toyota Corolla 71.1
Insight: Toyota Corolla has MIN DISP-‘71.1’
7. get summary of the data?
summary(mtcars_Data)
## model mpg cyl disp
## Length:32 Min. :10.40 Min. :4.000 Min. : 71.1
## Class :character 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8
## Mode :character Median :19.20 Median :6.000 Median :196.3
## Mean :20.09 Mean :6.188 Mean :230.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0
## Max. :33.90 Max. :8.000 Max. :472.0
## hp drat wt qsec
## Min. : 52.0 Min. :2.760 Min. :1.513 Min. :14.50
## 1st Qu.: 96.5 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89
## Median :123.0 Median :3.695 Median :3.325 Median :17.71
## Mean :146.7 Mean :3.597 Mean :3.217 Mean :17.85
## 3rd Qu.:180.0 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90
## Max. :335.0 Max. :4.930 Max. :5.424 Max. :22.90
## vs am gear carb
## Min. :0.0000 Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4375 Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :1.0000 Max. :5.000 Max. :8.000
9. out of all the cars with 4 cyl which car has max displ and min hp?
mtcars_Data %>%
filter((cyl==4))%>%
select(model,cyl,hp, disp)->mycars_data_processed # First i subset the data with given condition
mycars_data_processed %>%
filter((disp==max(disp)))%>%
select(model,cyl, disp) # Final processed the data
## model cyl disp
## 1 Merc 240D 4 146.7
Insight: Merc 240D has max disp among all 4 cyl car.
mycars_data_processed %>%
filter((hp==min(hp)))%>%
select(model,cyl,hp)
## model cyl hp
## 1 Honda Civic 4 52
Insight: Honda Civic has min hp among all 4 cyl car.
10. out of all the cars with 4 gear and >=4 cyl which car has max displ and min hp?
mtcars_Data %>%
filter((gear==4)& (cyl>=4))%>%
select(model,gear,cyl,hp, disp)->mycars_data_processed_2
mycars_data_processed_2 %>%
filter((disp==max(disp)))%>%
select(model,gear,cyl, disp)
## model gear cyl disp
## 1 Merc 280 4 6 167.6
## 2 Merc 280C 4 6 167.6
Insight: Merc 280 and Merc 280C has max disp among all the cars with 4 gear and >=4 cyl
mycars_data_processed_2 %>%
filter((hp==min(hp)))%>%
select(model,gear,cyl, hp)
## model gear cyl hp
## 1 Honda Civic 4 4 52
Insight: Honda Civic has min hp among all the cars with 4 gear and >=4 cyl
detach(mtcars_Data)