Database Used: mtcars.csv
BONUS - Place the original .csv in a github file and have R read from the link. This will be a very useful skill as you progress in your data science education and career.
library(RCurl)
## Loading required package: bitops
x <- getURL("https://raw.githubusercontent.com/jcp9010/R-Week-2-HW-Assignment/master/mtcars.csv")
mtcars.sample <- read.csv(text = x)
print(mtcars.sample)
## 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
## 21 Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## 22 Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## 23 AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## 24 Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## 25 Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## 26 Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## 27 Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## 28 Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## 29 Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## 30 Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## 31 Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## 32 Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
print(summary(mtcars.sample))
## 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
##
# Mean and Median for MPG of all cars within database mtcars.sample
print(paste0("The mean MPG of all cars within database mtcars.sample is: ", mean(mtcars.sample$mpg)))
## [1] "The mean MPG of all cars within database mtcars.sample is: 20.090625"
print(paste0("The median MPG of all cars within database mtcars.sample is: ", median(mtcars.sample$mpg)))
## [1] "The median MPG of all cars within database mtcars.sample is: 19.2"
print(paste0("The mean HP of all cars within database mtcars.sample is: ", mean(mtcars.sample$hp)))
## [1] "The mean HP of all cars within database mtcars.sample is: 146.6875"
print(paste0("The median HP of all cars within database mtcars.sample is: ", median(mtcars.sample$hp)))
## [1] "The median HP of all cars within database mtcars.sample is: 123"
# New data frame set called df.mtcars, which contains a subset of cars that have MPG > 20
df.mtcars <- subset(mtcars.sample, mpg > 20)
print(df.mtcars)
## 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
## 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
## 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
## 21 Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## 26 Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## 27 Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## 28 Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## 32 Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
If I were to create a brand new data.frame altogether, an example is shown below:
x <- 1:10
y <- letters[1:10]
z <- c(T,F,T,T,F,F,F,T,T,T)
a <- sample(1:100, 10)
b <- sample(1:1000, 10)
df <- data.frame(x,y,z,a, b)
print(df)
## x y z a b
## 1 1 a TRUE 81 242
## 2 2 b FALSE 35 500
## 3 3 c TRUE 62 446
## 4 4 d TRUE 3 175
## 5 5 e FALSE 5 818
## 6 6 f FALSE 26 393
## 7 7 g FALSE 78 284
## 8 8 h TRUE 46 678
## 9 9 i TRUE 1 841
## 10 10 j TRUE 22 101
colnames(df) <- c("Numbers","Letters","Boolean","Random1","Random2")
print(df)
## Numbers Letters Boolean Random1 Random2
## 1 1 a TRUE 81 242
## 2 2 b FALSE 35 500
## 3 3 c TRUE 62 446
## 4 4 d TRUE 3 175
## 5 5 e FALSE 5 818
## 6 6 f FALSE 26 393
## 7 7 g FALSE 78 284
## 8 8 h TRUE 46 678
## 9 9 i TRUE 1 841
## 10 10 j TRUE 22 101
df.mtcars$Power.To.Weight <- (df.mtcars$hp/df.mtcars$wt)
df.mtcars$Distance.Traveled.On.Ten.Gallons <- (df.mtcars$mpg * 10)
print(df.mtcars)
## 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
## 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
## 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
## 21 Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## 26 Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## 27 Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## 28 Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## 32 Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
## Power.To.Weight Distance.Traveled.On.Ten.Gallons
## 1 41.98473 210
## 2 38.26087 210
## 3 40.08621 228
## 4 34.21462 214
## 8 19.43574 244
## 9 30.15873 228
## 18 30.00000 324
## 19 32.19814 304
## 20 35.42234 339
## 21 39.35091 215
## 26 34.10853 273
## 27 42.52336 260
## 28 74.68605 304
## 32 39.20863 214
print(summary(df.mtcars))
## X mpg cyl disp
## Datsun 710 :1 Min. :21.00 Min. :4.000 Min. : 71.10
## Fiat 128 :1 1st Qu.:21.43 1st Qu.:4.000 1st Qu.: 83.03
## Fiat X1-9 :1 Median :23.60 Median :4.000 Median :120.20
## Honda Civic :1 Mean :25.48 Mean :4.429 Mean :123.89
## Hornet 4 Drive:1 3rd Qu.:29.62 3rd Qu.:4.000 3rd Qu.:145.22
## Lotus Europa :1 Max. :33.90 Max. :6.000 Max. :258.00
## (Other) :8
## hp drat wt qsec
## Min. : 52.0 Min. :3.080 Min. :1.513 Min. :16.46
## 1st Qu.: 66.0 1st Qu.:3.790 1st Qu.:1.986 1st Qu.:17.39
## Median : 94.0 Median :3.910 Median :2.393 Median :18.75
## Mean : 88.5 Mean :3.976 Mean :2.418 Mean :18.82
## 3rd Qu.:109.8 3rd Qu.:4.103 3rd Qu.:2.851 3rd Qu.:19.79
## Max. :113.0 Max. :4.930 Max. :3.215 Max. :22.90
##
## vs am gear carb
## Min. :0.0000 Min. :0.0000 Min. :3 Min. :1.000
## 1st Qu.:1.0000 1st Qu.:0.2500 1st Qu.:4 1st Qu.:1.000
## Median :1.0000 Median :1.0000 Median :4 Median :2.000
## Mean :0.7857 Mean :0.7143 Mean :4 Mean :1.857
## 3rd Qu.:1.0000 3rd Qu.:1.0000 3rd Qu.:4 3rd Qu.:2.000
## Max. :1.0000 Max. :1.0000 Max. :5 Max. :4.000
##
## Power.To.Weight Distance.Traveled.On.Ten.Gallons
## Min. :19.44 Min. :210.0
## 1st Qu.:32.68 1st Qu.:214.2
## Median :36.84 Median :236.0
## Mean :37.97 Mean :254.8
## 3rd Qu.:39.90 3rd Qu.:296.2
## Max. :74.69 Max. :339.0
##
print(paste0("The mean MPG of all cars within database mtcars.sample is: ", mean(mtcars.sample$mpg)))
## [1] "The mean MPG of all cars within database mtcars.sample is: 20.090625"
print(paste0("The mean MPG of all cars within database df.mtcars is: ", mean(df.mtcars$mpg)))
## [1] "The mean MPG of all cars within database df.mtcars is: 25.4785714285714"
print(paste0("The median MPG of all cars within database mtcars.sample is: ", median(mtcars.sample$mpg)))
## [1] "The median MPG of all cars within database mtcars.sample is: 19.2"
print(paste0("The median MPG of all cars within database df.mtcars is: ", median(df.mtcars$mpg)))
## [1] "The median MPG of all cars within database df.mtcars is: 23.6"
print(paste0("The mean HP of all cars within database mtcars.sample is: ", mean(mtcars.sample$hp)))
## [1] "The mean HP of all cars within database mtcars.sample is: 146.6875"
print(paste0("The mean HP of all cars within database df.mtcars is: ", mean(df.mtcars$hp)))
## [1] "The mean HP of all cars within database df.mtcars is: 88.5"
print(paste0("The median HP of all cars within database mtcars.sample is: ", median(mtcars.sample$hp)))
## [1] "The median HP of all cars within database mtcars.sample is: 123"
print(paste0("The median HP of all cars within database df.mtcars is: ", median(df.mtcars$hp)))
## [1] "The median HP of all cars within database df.mtcars is: 94"
i <- 1
for (row in df.mtcars$cyl){
if (row == 6){
df.mtcars[[i,'cyl']] <- "Six Cylinders"
}else if (row == 8){
df.mtcars[[i,'cyl']] <- "Eight Cylinders"
}else if (row == 4){
df.mtcars[[i,'cyl']] <- "Four Cylinders"
}
i <- i + 1
}
print(df.mtcars)
## X mpg cyl disp hp drat wt qsec vs am
## 1 Mazda RX4 21.0 Six Cylinders 160.0 110 3.90 2.620 16.46 0 1
## 2 Mazda RX4 Wag 21.0 Six Cylinders 160.0 110 3.90 2.875 17.02 0 1
## 3 Datsun 710 22.8 Four Cylinders 108.0 93 3.85 2.320 18.61 1 1
## 4 Hornet 4 Drive 21.4 Six Cylinders 258.0 110 3.08 3.215 19.44 1 0
## 8 Merc 240D 24.4 Four Cylinders 146.7 62 3.69 3.190 20.00 1 0
## 9 Merc 230 22.8 Four Cylinders 140.8 95 3.92 3.150 22.90 1 0
## 18 Fiat 128 32.4 Four Cylinders 78.7 66 4.08 2.200 19.47 1 1
## 19 Honda Civic 30.4 Four Cylinders 75.7 52 4.93 1.615 18.52 1 1
## 20 Toyota Corolla 33.9 Four Cylinders 71.1 65 4.22 1.835 19.90 1 1
## 21 Toyota Corona 21.5 Four Cylinders 120.1 97 3.70 2.465 20.01 1 0
## 26 Fiat X1-9 27.3 Four Cylinders 79.0 66 4.08 1.935 18.90 1 1
## 27 Porsche 914-2 26.0 Four Cylinders 120.3 91 4.43 2.140 16.70 0 1
## 28 Lotus Europa 30.4 Four Cylinders 95.1 113 3.77 1.513 16.90 1 1
## 32 Volvo 142E 21.4 Four Cylinders 121.0 109 4.11 2.780 18.60 1 1
## gear carb Power.To.Weight Distance.Traveled.On.Ten.Gallons
## 1 4 4 41.98473 210
## 2 4 4 38.26087 210
## 3 4 1 40.08621 228
## 4 3 1 34.21462 214
## 8 4 2 19.43574 244
## 9 4 2 30.15873 228
## 18 4 1 30.00000 324
## 19 4 2 32.19814 304
## 20 4 1 35.42234 339
## 21 3 1 39.35091 215
## 26 4 1 34.10853 273
## 27 5 2 42.52336 260
## 28 5 2 74.68605 304
## 32 4 2 39.20863 214
Also to demonstrate that the eight cylinders will take place for 8, I will run this code for mtcars.sample
i <- 1
for (row in mtcars.sample$cyl){
if (row == 6){
mtcars.sample[[i,'cyl']] <- "Six Cylinders"
}else if (row == 8){
mtcars.sample[[i,'cyl']] <- "Eight Cylinders"
}else if (row == 4){
mtcars.sample[[i,'cyl']] <- "Four Cylinders"
}
i <- i + 1
}
print(mtcars.sample)
## X mpg cyl disp hp drat wt qsec vs
## 1 Mazda RX4 21.0 Six Cylinders 160.0 110 3.90 2.620 16.46 0
## 2 Mazda RX4 Wag 21.0 Six Cylinders 160.0 110 3.90 2.875 17.02 0
## 3 Datsun 710 22.8 Four Cylinders 108.0 93 3.85 2.320 18.61 1
## 4 Hornet 4 Drive 21.4 Six Cylinders 258.0 110 3.08 3.215 19.44 1
## 5 Hornet Sportabout 18.7 Eight Cylinders 360.0 175 3.15 3.440 17.02 0
## 6 Valiant 18.1 Six Cylinders 225.0 105 2.76 3.460 20.22 1
## 7 Duster 360 14.3 Eight Cylinders 360.0 245 3.21 3.570 15.84 0
## 8 Merc 240D 24.4 Four Cylinders 146.7 62 3.69 3.190 20.00 1
## 9 Merc 230 22.8 Four Cylinders 140.8 95 3.92 3.150 22.90 1
## 10 Merc 280 19.2 Six Cylinders 167.6 123 3.92 3.440 18.30 1
## 11 Merc 280C 17.8 Six Cylinders 167.6 123 3.92 3.440 18.90 1
## 12 Merc 450SE 16.4 Eight Cylinders 275.8 180 3.07 4.070 17.40 0
## 13 Merc 450SL 17.3 Eight Cylinders 275.8 180 3.07 3.730 17.60 0
## 14 Merc 450SLC 15.2 Eight Cylinders 275.8 180 3.07 3.780 18.00 0
## 15 Cadillac Fleetwood 10.4 Eight Cylinders 472.0 205 2.93 5.250 17.98 0
## 16 Lincoln Continental 10.4 Eight Cylinders 460.0 215 3.00 5.424 17.82 0
## 17 Chrysler Imperial 14.7 Eight Cylinders 440.0 230 3.23 5.345 17.42 0
## 18 Fiat 128 32.4 Four Cylinders 78.7 66 4.08 2.200 19.47 1
## 19 Honda Civic 30.4 Four Cylinders 75.7 52 4.93 1.615 18.52 1
## 20 Toyota Corolla 33.9 Four Cylinders 71.1 65 4.22 1.835 19.90 1
## 21 Toyota Corona 21.5 Four Cylinders 120.1 97 3.70 2.465 20.01 1
## 22 Dodge Challenger 15.5 Eight Cylinders 318.0 150 2.76 3.520 16.87 0
## 23 AMC Javelin 15.2 Eight Cylinders 304.0 150 3.15 3.435 17.30 0
## 24 Camaro Z28 13.3 Eight Cylinders 350.0 245 3.73 3.840 15.41 0
## 25 Pontiac Firebird 19.2 Eight Cylinders 400.0 175 3.08 3.845 17.05 0
## 26 Fiat X1-9 27.3 Four Cylinders 79.0 66 4.08 1.935 18.90 1
## 27 Porsche 914-2 26.0 Four Cylinders 120.3 91 4.43 2.140 16.70 0
## 28 Lotus Europa 30.4 Four Cylinders 95.1 113 3.77 1.513 16.90 1
## 29 Ford Pantera L 15.8 Eight Cylinders 351.0 264 4.22 3.170 14.50 0
## 30 Ferrari Dino 19.7 Six Cylinders 145.0 175 3.62 2.770 15.50 0
## 31 Maserati Bora 15.0 Eight Cylinders 301.0 335 3.54 3.570 14.60 0
## 32 Volvo 142E 21.4 Four Cylinders 121.0 109 4.11 2.780 18.60 1
## am gear carb
## 1 1 4 4
## 2 1 4 4
## 3 1 4 1
## 4 0 3 1
## 5 0 3 2
## 6 0 3 1
## 7 0 3 4
## 8 0 4 2
## 9 0 4 2
## 10 0 4 4
## 11 0 4 4
## 12 0 3 3
## 13 0 3 3
## 14 0 3 3
## 15 0 3 4
## 16 0 3 4
## 17 0 3 4
## 18 1 4 1
## 19 1 4 2
## 20 1 4 1
## 21 0 3 1
## 22 0 3 2
## 23 0 3 2
## 24 0 3 4
## 25 0 3 2
## 26 1 4 1
## 27 1 5 2
## 28 1 5 2
## 29 1 5 4
## 30 1 5 6
## 31 1 5 8
## 32 1 4 2