Data Set mtcars Exploration
data(mtcars)
#Summary of mtcars
str(mtcars)
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
#First 6 Rows of mtcars
head(mtcars)
#Classify Efficient Cars
mtcars$mpg>20 & mtcars$cyl==4
## [1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE
## [25] FALSE TRUE TRUE TRUE FALSE FALSE FALSE TRUE
#Flag Performance Cars
mtcars$hp>150 | mtcars$wt<2.5
## [1] FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE
## [13] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE
## [25] TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE
#Transmission-Based Anaysis
ifelse(mtcars$am == 0 & mtcars$mpg > 20,"Auto-High",NA)
## [1] NA NA NA "Auto-High" NA NA
## [7] NA "Auto-High" "Auto-High" NA NA NA
## [13] NA NA NA NA NA NA
## [19] NA NA "Auto-High" NA NA NA
## [25] NA NA NA NA NA NA
## [31] NA NA
ifelse(mtcars$am == 1 & mtcars$mpg > 20,"Manual-High","Low")
## [1] "Manual-High" "Manual-High" "Manual-High" "Low" "Low"
## [6] "Low" "Low" "Low" "Low" "Low"
## [11] "Low" "Low" "Low" "Low" "Low"
## [16] "Low" "Low" "Manual-High" "Manual-High" "Manual-High"
## [21] "Low" "Low" "Low" "Low" "Low"
## [26] "Manual-High" "Manual-High" "Manual-High" "Low" "Low"
## [31] "Low" "Manual-High"
#Custom Score Using "If Else"
ifelse(mtcars$gear == 4,mtcars$hp + mtcars$mpg,mtcars$hp - mtcars$wt)
## [1] 131.000 131.000 115.800 106.785 171.560 101.540 241.430 86.400 117.800
## [10] 142.200 140.800 175.930 176.270 176.220 199.750 209.576 224.655 98.400
## [19] 82.400 98.900 94.535 146.480 146.565 241.160 171.155 93.300 88.860
## [28] 111.487 260.830 172.230 331.430 130.400
#Compound Decision
for (i in 1:nrow(mtcars)) {if (mtcars$cyl[i] == 8 && mtcars$mpg[i] < 15) {print("High power, low efficiency.")} else if (mtcars$cyl[i] < 6 && mtcars$mpg[i] > 25) {print("Efficient compact.")} else {print("Standard configuration.")}}
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "High \tpower, low efficiency."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "High \tpower, low efficiency."
## [1] "High \tpower, low efficiency."
## [1] "High \tpower, low efficiency."
## [1] "Efficient compact."
## [1] "Efficient compact."
## [1] "Efficient compact."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "High \tpower, low efficiency."
## [1] "Standard configuration."
## [1] "Efficient compact."
## [1] "Efficient compact."
## [1] "Efficient compact."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "Standard configuration."
## [1] "Standard configuration."