Load Required Libraries
#Load essential packages
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
Descriptive Analysis
selected_data <- mtcars %>%
select(mpg, cyl, hp, wt)
print("Selected Columns:")
## [1] "Selected Columns:"
print(head(selected_data))
## mpg cyl hp wt
## Mazda RX4 21.0 6 110 2.620
## Mazda RX4 Wag 21.0 6 110 2.875
## Datsun 710 22.8 4 93 2.320
## Hornet 4 Drive 21.4 6 110 3.215
## Hornet Sportabout 18.7 8 175 3.440
## Valiant 18.1 6 105 3.460
filtered_data <- mtcars %>%
filter(mpg > 20)
print("Filtered Data (MPG > 20):")
## [1] "Filtered Data (MPG > 20):"
print(head(filtered_data))
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
arranged_data <- mtcars %>%
arrange(desc(mpg))
print("Arranged by MPG (Descending):")
## [1] "Arranged by MPG (Descending):"
print(head(arranged_data))
## mpg cyl disp hp drat wt qsec vs am gear carb
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
plot(mtcars$hp, mtcars$mpg,
main = "MPG vs Horsepower",
xlab = "Horsepower", ylab = "Miles per Gallon",
col = "blue", pch = 19)

barplot(table(mtcars$cyl),
main = "Count of Cars by Cylinder",
xlab = "Cylinders", ylab = "Number of Cars",
col = "orange")

boxplot(mpg ~ cyl, data = mtcars,
main = "MPG by Number of Cylinders",
xlab = "Cylinders", ylab = "Miles Per Gallon",
col = "lightgreen")

hist(mtcars$mpg,
main = "Histogram of MPG",
xlab = "Miles Per Gallon",
col = "lightblue", border = "black")

corr_matrix <- cor(mtcars[, c("mpg", "hp", "wt", "disp")])
heatmap(corr_matrix, main = "Correlation Heatmap", col = heat.colors(256))
