Reg.No:-12222038 Roll.No:- 56
library("tidyverse")
## Warning: package 'ggplot2' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library("tidyr")
library("dplyr")
library("ggplot2")
library(readxl)
VEHICLE_DATASET <- read_csv("C:/Users/aquib/Desktop/VEHICLE DATASET.csv")
## Rows: 1099 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): Make, Model, Fuel_type, Transmission, Location, Color, Owner, Engin...
## dbl (5): Price, Year, Kilometer, Seating_capacity, Fuel_tank_capacity
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(VEHICLE_DATASET)
Q1) Omit all the NA values from the dataset ?
Vehicle <- na.omit(VEHICLE_DATASET)
print(Vehicle)
## # A tibble: 994 × 14
## Make Model Price Year Kilometer Fuel_type Transmission Location Color
## <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr>
## 1 Honda Amaz… 5.05e5 2017 87150 Petrol Manual Pune Grey
## 2 Maruti Su… Swif… 4.50e5 2014 75000 Diesel Manual Ludhiana White
## 3 Hyundai i10 … 2.20e5 2011 67000 Petrol Manual Lucknow Maro…
## 4 Toyota Glan… 7.99e5 2019 37500 Petrol Manual Mangalo… Red
## 5 Toyota Inno… 1.95e6 2018 69000 Diesel Manual Mumbai Grey
## 6 Maruti Su… Ciaz… 6.75e5 2017 73315 Petrol Manual Pune Grey
## 7 BMW X1 x… 2.65e6 2017 75000 Diesel Automatic Coimbat… White
## 8 Skoda Octa… 1.39e6 2017 56000 Petrol Automatic Mumbai White
## 9 Nissan Terr… 5.75e5 2015 85000 Diesel Manual Mumbai White
## 10 Hyundai Elit… 5.91e5 2017 20281 Petrol Manual Mumbai Red
## # ℹ 984 more rows
## # ℹ 5 more variables: Owner <chr>, Engine <chr>, Drivetrain <chr>,
## # Seating_capacity <dbl>, Fuel_tank_capacity <dbl>
Q2) Find the Vehicle which runs the most km?
vehicle_with_max_km <- Vehicle[which.max(Vehicle$Kilometer),]
print(vehicle_with_max_km)
## # A tibble: 1 × 14
## Make Model Price Year Kilometer Fuel_type Transmission Location Color Owner
## <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr> <chr>
## 1 Audi A6 3… 1.65e6 2012 240000 Diesel Automatic Lucknow White First
## # ℹ 4 more variables: Engine <chr>, Drivetrain <chr>, Seating_capacity <dbl>,
## # Fuel_tank_capacity <dbl>
Q3) Find the average km run by the vehicles from the dataset?
average_km <- mean(Vehicle$Kilometer, na.rm = TRUE)
print(average_km)
## [1] 51431.83
Q4) Extract all the car of nissan and store it in new object in the dataset ?
nissan_cars <- subset(Vehicle, Make == "Nissan")
print(nissan_cars)
## # A tibble: 8 × 14
## Make Model Price Year Kilometer Fuel_type Transmission Location Color Owner
## <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr> <chr>
## 1 Niss… Terr… 575000 2015 85000 Diesel Manual Mumbai White First
## 2 Niss… Terr… 650000 2016 76584 Diesel Manual Mumbai White First
## 3 Niss… Terr… 450000 2014 85000 Diesel Manual Hyderab… Silv… Seco…
## 4 Niss… Micr… 490000 2014 56918 Petrol Automatic Coimbat… Blue Seco…
## 5 Niss… Terr… 600000 2016 99000 Diesel Manual Chennai White First
## 6 Niss… Sunn… 360000 2013 92000 Diesel Manual Lucknow Grey Seco…
## 7 Niss… Sunn… 325000 2013 51000 Diesel Manual Zirakpur Grey First
## 8 Niss… Magn… 625000 2021 28000 Petrol Manual Zirakpur White First
## # ℹ 4 more variables: Engine <chr>, Drivetrain <chr>, Seating_capacity <dbl>,
## # Fuel_tank_capacity <dbl>
Q5) Subset the dataset Vehicle which should have 290 observations and attributes Make , Price , Transmission and color ?
subset <- Vehicle %>% select(Make, Price, Transmission , Color) %>% slice(1:290)
print(subset)
## # A tibble: 290 × 4
## Make Price Transmission Color
## <chr> <dbl> <chr> <chr>
## 1 Honda 505000 Manual Grey
## 2 Maruti Suzuki 450000 Manual White
## 3 Hyundai 220000 Manual Maroon
## 4 Toyota 799000 Manual Red
## 5 Toyota 1950000 Manual Grey
## 6 Maruti Suzuki 675000 Manual Grey
## 7 BMW 2650000 Automatic White
## 8 Skoda 1390000 Automatic White
## 9 Nissan 575000 Manual White
## 10 Hyundai 591000 Manual Red
## # ℹ 280 more rows
Q6) Filter out all the cars from the dataset which runs more than 100000 kms and maker is toyota ?
toyota <- Vehicle %>% filter(Kilometer > 100000 , Make == "Toyota")
print(toyota)
## # A tibble: 14 × 14
## Make Model Price Year Kilometer Fuel_type Transmission Location Color
## <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr>
## 1 Toyota Fortuner… 1.4 e6 2014 143000 Diesel Automatic Faridab… White
## 2 Toyota Fortuner… 1.68e6 2014 211000 Diesel Manual Jaipur White
## 3 Toyota Innova 2… 1.58e6 2018 115000 Diesel Automatic Delhi Beige
## 4 Toyota Fortuner… 7.5 e5 2011 117000 Diesel Manual Agra White
## 5 Toyota Corolla … 7.25e5 2014 104814 Petrol Manual Mumbai Grey
## 6 Toyota Innova 2… 7.5 e5 2014 126000 Diesel Manual Delhi Silv…
## 7 Toyota Fortuner… 1.29e6 2012 121000 Diesel Manual Ludhiana Gold
## 8 Toyota Fortuner… 1.08e6 2013 125000 Diesel Automatic Delhi White
## 9 Toyota Etios G 3.25e5 2011 101347 Petrol Manual Pune Red
## 10 Toyota Fortuner… 8.25e5 2010 114000 Diesel Manual Lucknow Black
## 11 Toyota Fortuner… 1.6 e6 2015 145000 Diesel Automatic Mohali Gold
## 12 Toyota Innova 2… 2.10e6 2017 112000 Diesel Manual Hyderab… Bron…
## 13 Toyota Innova 2… 2.04e6 2018 110000 Diesel Automatic Delhi Bron…
## 14 Toyota Fortuner… 1.75e6 2014 101000 Diesel Automatic Jalandh… White
## # ℹ 5 more variables: Owner <chr>, Engine <chr>, Drivetrain <chr>,
## # Seating_capacity <dbl>, Fuel_tank_capacity <dbl>
Q7) Calculate the total kilometers driven for each maker of vehicle
from the Vehicle
dataset and store it in new object?
total_kilometers <- Vehicle %>%
group_by(Make) %>%
summarise(Total_Kilometers = sum(Kilometer))
print(total_kilometers)
## # A tibble: 32 × 2
## Make Total_Kilometers
## <chr> <dbl>
## 1 Audi 3929105
## 2 BMW 2302351
## 3 Chevrolet 73265
## 4 Datsun 135313
## 5 Ferrari 9500
## 6 Fiat 71000
## 7 Ford 1200520
## 8 Honda 4287298
## 9 Hyundai 8147293
## 10 Isuzu 171687
## # ℹ 22 more rows
Q8) Identify the most common engine types used in vehicles from the Vehicle dataset?
common_engines <- Vehicle %>%count(Engine) %>% arrange(desc(n))
print(common_engines)
## # A tibble: 95 × 2
## Engine n
## <chr> <int>
## 1 1197 cc 127
## 2 998 cc 57
## 3 1248 cc 56
## 4 1968 cc 47
## 5 1497 cc 46
## 6 1995 cc 41
## 7 2179 cc 37
## 8 1498 cc 31
## 9 2143 cc 27
## 10 1199 cc 26
## # ℹ 85 more rows
Q9) What is the average price of vehicles for each Owner type and How many vehicles are there in each location ?
average_price_owner <- Vehicle %>%
group_by(Owner) %>%
summarise(Avg_Price = mean(Price))
print(average_price_owner)
## # A tibble: 4 × 2
## Owner Avg_Price
## <chr> <dbl>
## 1 First 1774048.
## 2 Second 1444253.
## 3 Third 1991600
## 4 UnRegistered Car 4046778.
vehicle_count_location <- Vehicle %>% count(Location)
print(vehicle_count_location)
## # A tibble: 68 × 2
## Location n
## <chr> <int>
## 1 Agra 8
## 2 Ahmedabad 29
## 3 Allahabad 1
## 4 Ambala Cantt 4
## 5 Amritsar 1
## 6 Aurangabad 3
## 7 Bangalore 65
## 8 Bhopal 2
## 9 Bhubaneswar 2
## 10 Chandigarh 10
## # ℹ 58 more rows
Q10) Create a new column to categorize vehicles into age groups (e.g., “Old”, “Medium”, “New”) based on their manufacturing year?
vehicle_age <- Vehicle %>%
mutate(Age_Group = case_when(
Year >= 2010 ~ "New",
Year >= 2000 & Year < 2010 ~ "Medium",
TRUE ~ "Old" ))
print(vehicle_age)
## # A tibble: 994 × 15
## Make Model Price Year Kilometer Fuel_type Transmission Location Color
## <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <chr> <chr>
## 1 Honda Amaz… 5.05e5 2017 87150 Petrol Manual Pune Grey
## 2 Maruti Su… Swif… 4.50e5 2014 75000 Diesel Manual Ludhiana White
## 3 Hyundai i10 … 2.20e5 2011 67000 Petrol Manual Lucknow Maro…
## 4 Toyota Glan… 7.99e5 2019 37500 Petrol Manual Mangalo… Red
## 5 Toyota Inno… 1.95e6 2018 69000 Diesel Manual Mumbai Grey
## 6 Maruti Su… Ciaz… 6.75e5 2017 73315 Petrol Manual Pune Grey
## 7 BMW X1 x… 2.65e6 2017 75000 Diesel Automatic Coimbat… White
## 8 Skoda Octa… 1.39e6 2017 56000 Petrol Automatic Mumbai White
## 9 Nissan Terr… 5.75e5 2015 85000 Diesel Manual Mumbai White
## 10 Hyundai Elit… 5.91e5 2017 20281 Petrol Manual Mumbai Red
## # ℹ 984 more rows
## # ℹ 6 more variables: Owner <chr>, Engine <chr>, Drivetrain <chr>,
## # Seating_capacity <dbl>, Fuel_tank_capacity <dbl>, Age_Group <chr>
Q11) From the Vehicle dataset Visualize the relationship between maker and transmission?
ggplot(Vehicle, aes(x = Make, y = Transmission)) +
geom_point(color = "purple") +
labs(title = "Relationship between Maker and Transmission type",
x = "Maker", y = "Transmission type")
Q12) What is the average fuel tank capacity for each maker of vehicle also show the data using bar plot for better comparison ?
average_fuel_capacity_make <- Vehicle %>%
group_by(Make) %>%
summarise(Avg_Fuel_Capacity = mean(Fuel_tank_capacity))
print(average_fuel_capacity_make)
## # A tibble: 32 × 2
## Make Avg_Fuel_Capacity
## <chr> <dbl>
## 1 Audi 65.9
## 2 BMW 63.2
## 3 Chevrolet 60
## 4 Datsun 31.5
## 5 Ferrari 78
## 6 Fiat 45
## 7 Ford 54.3
## 8 Honda 39.5
## 9 Hyundai 46.2
## 10 Isuzu 65
## # ℹ 22 more rows
ggplot(average_fuel_capacity_make, aes(x = Make, y = Avg_Fuel_Capacity)) +
geom_bar(stat = "identity", fill = "green") +
labs(title = "Average Fuel Tank Capacity by Maker",
x = "Make", y = "Average Fuel Tank Capacity")
Q13) What is the trend of vehicle prices over the years?
ggplot(Vehicle, aes(x = Year, y = Price)) + geom_smooth()+labs(title = "Trend of Vehicle Prices Over Years", x = "Year", y = "Price")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Q14) Which fuel type is most common among all the vehicles in the vehicle_dataset represent it using a bar plot ?
ggplot(Vehicle, aes(x = Fuel_type, fill = Fuel_type)) +geom_bar() +
labs(title = "Distribution of Fuel Types", x = "Fuel Type", y = "Count")
Q15)What is the distribution of drivetrain types among different vehicle owners?
ggplot(Vehicle, aes(x = Owner, fill = Drivetrain)) +
geom_bar() + labs(title = "Distribution of Drivetrain Types Across Vehicle Owners",
x = "Owner", y = "Count")