Import the data set “mtcars” from the wroking directory.
mtcars <- read.csv(file="mtcars.csv", header=TRUE, sep=",")
**This open source dataset is downloaded from http://vincentarelbundock.github.io/Rdatasets/
Description: The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).
Format: A data frame with 32 observations on 11 (numeric) variables.
[, 1] mpg Miles/(US) gallon
[, 2] cyl Number of cylinders
[, 3] disp Displacement (cu.in.) [, 4] hp Gross horsepower
[, 5] drat Rear axle ratio
[, 6] wt Weight (1000 lbs)
[, 7] qsec 1/4 mile time
[, 8] vs Engine (0 = V-shaped, 1 = straight)
[, 9] am Transmission (0 = automatic, 1 = manual)
[,10] gear Number of forward gears
[,11] carb Number of carburetors
Summary:
summary(mtcars)
## X mpg cyl disp
## AMC Javelin : 1 Min. :10.40 Min. :4.000 Min. : 71.1
## Cadillac Fleetwood: 1 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8
## Camaro Z28 : 1 Median :19.20 Median :6.000 Median :196.3
## Chrysler Imperial : 1 Mean :20.09 Mean :6.188 Mean :230.7
## Datsun 710 : 1 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0
## Dodge Challenger : 1 Max. :33.90 Max. :8.000 Max. :472.0
## (Other) :26
## 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
##
The mean of mpg
mean(mtcars$mpg)
## [1] 20.09062
The median of mpg
median(mtcars$mpg)
## [1] 19.2
The mean of hp
mean(mtcars$hp)
## [1] 146.6875
The median of hp
median(mtcars$hp)
## [1] 123
The mean of wt
mean(mtcars$wt)
## [1] 3.21725
The median of wt
median(mtcars$wt)
## [1] 3.325
newDataSet <- data.frame(mtcars$mpg, mtcars$hp, mtcars$wt, mtcars$gear)
row.names(newDataSet) <- row.names(mtcars)
colnames(newDataSet) <- c("Miles_Per_Gallon","Gross_Horsepower", "Weight_In_1000_LBS", "Number_Of_Forward_Gears")
Summary:
summary(newDataSet)
## Miles_Per_Gallon Gross_Horsepower Weight_In_1000_LBS
## Min. :10.40 Min. : 52.0 Min. :1.513
## 1st Qu.:15.43 1st Qu.: 96.5 1st Qu.:2.581
## Median :19.20 Median :123.0 Median :3.325
## Mean :20.09 Mean :146.7 Mean :3.217
## 3rd Qu.:22.80 3rd Qu.:180.0 3rd Qu.:3.610
## Max. :33.90 Max. :335.0 Max. :5.424
## Number_Of_Forward_Gears
## Min. :3.000
## 1st Qu.:3.000
## Median :4.000
## Mean :3.688
## 3rd Qu.:4.000
## Max. :5.000
The mean of Miles_Per_Gallon
mean(newDataSet$Miles_Per_Gallon)
## [1] 20.09062
The median of Miles_Per_Gallon
median(newDataSet$Miles_Per_Gallon)
## [1] 19.2
The mean of Gross_Horsepower
mean(newDataSet$Gross_Horsepower)
## [1] 146.6875
The median of Gross_Horsepower
median(newDataSet$Gross_Horsepower)
## [1] 123
The mean of Weight_In_1000_LBS
mean(newDataSet$Weight_In_1000_LBS)
## [1] 3.21725
The median of Weight_In_1000_LBS
median(newDataSet$Weight_In_1000_LBS)
## [1] 3.325
The result show that the statistics (including the mean and median) of the attributes in the new data frame are the same as the corresponding attributes in the orginal data frame
newDataSet$Number_Of_Forward_Gears[newDataSet$Number_Of_Forward_Gears == 1] <- "One"
newDataSet$Number_Of_Forward_Gears[newDataSet$Number_Of_Forward_Gears == 2] <- "Two"
newDataSet$Number_Of_Forward_Gears[newDataSet$Number_Of_Forward_Gears == 3] <- "Three"
newDataSet$Number_Of_Forward_Gears[newDataSet$Number_Of_Forward_Gears == 4] <- "Four"
newDataSet$Number_Of_Forward_Gears[newDataSet$Number_Of_Forward_Gears == 5] <- "Five"
mtcars[1:20,]
## X mpg cyl disp hp drat wt qsec vs am gear carb
## 1 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## 2 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## 3 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## 4 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## 5 Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## 6 Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## 7 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## 8 Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## 9 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## 10 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## 11 Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## 12 Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## 13 Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## 14 Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## 15 Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## 16 Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## 17 Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## 18 Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## 19 Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## 20 Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
newDataSet[1:20,]
## Miles_Per_Gallon Gross_Horsepower Weight_In_1000_LBS
## 1 21.0 110 2.620
## 2 21.0 110 2.875
## 3 22.8 93 2.320
## 4 21.4 110 3.215
## 5 18.7 175 3.440
## 6 18.1 105 3.460
## 7 14.3 245 3.570
## 8 24.4 62 3.190
## 9 22.8 95 3.150
## 10 19.2 123 3.440
## 11 17.8 123 3.440
## 12 16.4 180 4.070
## 13 17.3 180 3.730
## 14 15.2 180 3.780
## 15 10.4 205 5.250
## 16 10.4 215 5.424
## 17 14.7 230 5.345
## 18 32.4 66 2.200
## 19 30.4 52 1.615
## 20 33.9 65 1.835
## Number_Of_Forward_Gears
## 1 Four
## 2 Four
## 3 Four
## 4 Three
## 5 Three
## 6 Three
## 7 Three
## 8 Four
## 9 Four
## 10 Four
## 11 Four
## 12 Three
## 13 Three
## 14 Three
## 15 Three
## 16 Three
## 17 Three
## 18 Four
## 19 Four
## 20 Four
library(readr)
gitHubMtcars <- read.csv("https://raw.githubusercontent.com/ezaccountz/SPS_Bridge_R_HW2/master/mtcars.csv")