# Check if 'openxlsx' is installed and install if necessary
if (!require(openxlsx)) {
install.packages("openxlsx")
}Loading required package: openxlsx
# Load the necessary package to read Excel files
library(openxlsx)# for main sections and ## for subsections.# Data Loading and ## Exploratory Data Analysis to organize your analysis.openxlsx is Installed and Loading Necessary Librariesopenxlsx package is installed. If it is not installed, we will install it. After that, we can load the package.# Check if 'openxlsx' is installed and install if necessary
if (!require(openxlsx)) {
install.packages("openxlsx")
}Loading required package: openxlsx
# Load the necessary package to read Excel files
library(openxlsx)read.xlsx() function from the openxlsx package. This dataset contains car data with various attributes, including Energy Efficiency (MPG) and Horsepower.# Load the dataset from an Excel file hosted online
car_data <- read.xlsx(
"https://ljkelly3141.github.io/real-world-statistics-with-r/data/car_price.xlsx"
)# Preview the first few rows of the dataset to ensure it's loaded correctly
head(car_data) Brand Model Trim Trim.Level Style Size MSRP.(USD)
1 Toyota Camry LE Base Sedan Midsize 29000
2 Toyota Camry XSE Medium Sedan Midsize 34000
3 Toyota Camry Hybrid Premium Sedan Midsize 37000
4 Ford F-150 XLT Base Pickup Full-size 52000
5 Ford F-150 Lariat Medium Pickup Full-size 61000
6 Ford F-150 Platinum Premium Pickup Full-size 72000
Energy.Efficiency.(MPG) Horsepower Engine.Size.(L) Customer.Rating
1 32 203 2.5 4.5
2 31 301 3.5 4.7
3 50 208 2.5 4.8
4 20 290 3.3 4.4
5 18 400 5.0 4.6
6 18 400 5.0 4.8
Safety.Rating Hybrid Electric Four_Wheel_Drive Sunroof Bluetooth
1 5 Non-Hybrid Non-Electric 2WD Sunroof Bluetooth
2 5 Non-Hybrid Non-Electric 2WD Sunroof Bluetooth
3 5 Hybrid <NA> 2WD Sunroof Bluetooth
4 5 Non-Hybrid <NA> 4WD <NA> Bluetooth
5 5 Non-Hybrid <NA> 4WD Sunroof Bluetooth
6 5 Non-Hybrid <NA> 4WD Sunroof Bluetooth
Backup_Camera Main.Market Average.Annual.Cost.of.Ownership.(USD)
1 Backup Camera North America 6200
2 Backup Camera North America 6400
3 Backup Camera North America 5800
4 Backup Camera North America 9100
5 Backup Camera North America 9500
6 Backup Camera North America 9800
str() function helps us see the data types of each column.# Check the structure of the dataset to see data types and variable names
str(car_data)'data.frame': 44 obs. of 20 variables:
$ Brand : chr "Toyota" "Toyota" "Toyota" "Ford" ...
$ Model : chr "Camry" "Camry" "Camry" "F-150" ...
$ Trim : chr "LE" "XSE" "Hybrid" "XLT" ...
$ Trim.Level : chr "Base" "Medium" "Premium" "Base" ...
$ Style : chr "Sedan" "Sedan" "Sedan" "Pickup" ...
$ Size : chr "Midsize" "Midsize" "Midsize" "Full-size" ...
$ MSRP.(USD) : num 29000 34000 37000 52000 61000 72000 53000 70000 25000 32000 ...
$ Energy.Efficiency.(MPG) : num 32 31 50 20 18 18 22 20 36 35 ...
$ Horsepower : num 203 301 208 290 400 400 355 420 158 180 ...
$ Engine.Size.(L) : num 2.5 3.5 2.5 3.3 5 5 5.3 6.2 2 1.5 ...
$ Customer.Rating : num 4.5 4.7 4.8 4.4 4.6 4.8 4.4 4.7 4.5 4.6 ...
$ Safety.Rating : num 5 5 5 5 5 5 4 4 5 5 ...
$ Hybrid : chr "Non-Hybrid" "Non-Hybrid" "Hybrid" "Non-Hybrid" ...
$ Electric : chr "Non-Electric" "Non-Electric" NA NA ...
$ Four_Wheel_Drive : chr "2WD" "2WD" "2WD" "4WD" ...
$ Sunroof : chr "Sunroof" "Sunroof" "Sunroof" NA ...
$ Bluetooth : chr "Bluetooth" "Bluetooth" "Bluetooth" "Bluetooth" ...
$ Backup_Camera : chr "Backup Camera" "Backup Camera" "Backup Camera" "Backup Camera" ...
$ Main.Market : chr "North America" "North America" "North America" "North America" ...
$ Average.Annual.Cost.of.Ownership.(USD): num 6200 6400 5800 9100 9500 9800 8800 9200 5600 5900 ...
Now that the dataset is loaded and understood, the next lecture will focus on performing Exploratory Data Analysis (EDA) to visualize the relationship between Energy Efficiency (MPG) and Horsepower.