Step 1: Data Import and Exploration

##load in data
df <- read.csv("mtcars-3.csv")

Data Understanding Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

##Displaying the first few rows using head() function
head(df)
##               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
##Print the dimension of the dataset
dim(df)
## [1] 32 12
##Print the data structure of variable df
class(df)
## [1] "data.frame"
##Print data types of column model, mpg,hp and am
model <- df[, c("model")]
class(model)
## [1] "character"
mpg <- df[, c("mpg")]
class(mpg)
## [1] "numeric"
hp <- df[, c("hp")]
class(hp)
## [1] "integer"
am <- df[, c("am")]
class(am)
## [1] "integer"
##Use summary() function to get some details of this dataset
summary(df)
##     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
##Change the datatype/class of variable ‘am’ from integer to boolean/logical
am <- as.logical(am)
class(am)
## [1] "logical"

Data Visualization

  1. Scatter plot
plot(hp, mpg,type = "p", main = "Horsepower VS Miles per Gallon")

The plot shows a negative correlation between horsepower and miles per gallon. The higher the horsepower, the less miles per gallon are consumed by the car.

  1. Bar Chart
cyl <- df[, c("cyl")]
plot(as.factor(cyl), main = "Distribution of Cars with Different Sets of Cylinders")

  1. Histogram
hist(mpg, main = "Miles per Gallon Histogram")