Completed homework should be submitted on CoursePlus as an Rmd file. Please see the course website for more information about submitting assignments: https://jhudatascience.org/intro_to_r/syllabus.html#submitting-assignments.
Homework will be graded for correct output, not code style. All assignments are due at the end of the course. Please see the course website for more information about grading: https://jhudatascience.org/intro_to_r/syllabus.html#grading.
## you can add more, or change...
library(tidyverse)
1. Create the following two objects.
bday <- "19-Feb"
name <- "Bruce Wayne"
2. Make an object “me” that is “bday” and “name” combined as a vector.
me <- c(bday, name)
3. Determine the data class for “me”.
class(me)
## [1] "character"
# The class for "me" is "character"
4. If I want to do me / 2
I get the following error: Error in me/2 : non-numeric argument to binary operator
. Why? Write your answer as a comment inside the R chunk below.
# R cannot perform math functions on character data classes (types).
The following questions involve an outside dataset.
We will be working with a dataset from the “Kaggle” website, which hosts competitions for prediction and machine learning. More details on this dataset are here: https://www.kaggle.com/c/DontGetKicked/overview/background.
5. Bring the dataset into R. The dataset is located at: https://jhudatascience.org/intro_to_r/data/kaggleCarAuction.csv. You can use the link, download it, or use whatever method you like for getting the file. Once you get the file, read the dataset in using read_csv()
and assign it the name cars
.
cars <- read_csv(file = "https://jhudatascience.org/intro_to_r/data/kaggleCarAuction.csv")
## Rows: 72983 Columns: 34
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (24): PurchDate, Auction, Make, Model, Trim, SubModel, Color, Transmissi...
## dbl (10): RefId, IsBadBuy, VehYear, VehicleAge, VehOdo, BYRNO, VNZIP1, VehBC...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# OR
cars <- read_csv("https://jhudatascience.org/intro_to_r/data/kaggleCarAuction.csv")
## Rows: 72983 Columns: 34
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (24): PurchDate, Auction, Make, Model, Trim, SubModel, Color, Transmissi...
## dbl (10): RefId, IsBadBuy, VehYear, VehicleAge, VehOdo, BYRNO, VNZIP1, VehBC...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# OR
url <- "https://jhudatascience.org/intro_to_r/data/kaggleCarAuction.csv"
cars <- read_csv(file = url)
## Rows: 72983 Columns: 34
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (24): PurchDate, Auction, Make, Model, Trim, SubModel, Color, Transmissi...
## dbl (10): RefId, IsBadBuy, VehYear, VehicleAge, VehOdo, BYRNO, VNZIP1, VehBC...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# OR
download.file(
url = "https://jhudatascience.org/intro_to_r/data/kaggleCarAuction.csv",
destfile = "cars_data.csv",
overwrite = TRUE,
mode = "wb"
)
cars <- read_csv(file = "cars_data.csv")
## Rows: 72983 Columns: 34
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (24): PurchDate, Auction, Make, Model, Trim, SubModel, Color, Transmissi...
## dbl (10): RefId, IsBadBuy, VehYear, VehicleAge, VehOdo, BYRNO, VNZIP1, VehBC...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
6. Import the data “dictionary” from https://jhudatascience.org/intro_to_r/data/Carvana_Data_Dictionary_formatted.txt. Use the read_tsv()
function and assign it the name “key”.
key <- read_tsv(file = "https://jhudatascience.org/intro_to_r/data/Carvana_Data_Dictionary_formatted.txt")
## Rows: 36 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (2): Field Name, Definition
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# OR
download.file(
url = "https://jhudatascience.org/intro_to_r/data/Carvana_Data_Dictionary_formatted.txt",
destfile = "dict.txt",
overwrite = TRUE,
mode = "wb"
)
key <- read_tsv("dict.txt")
## Rows: 36 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (2): Field Name, Definition
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
7. You should now be ready to work with the “cars” dataset.
str()
. Write your answer as a comment inside the R chunk below.# Use View(), look in the environment, or use another function like glimpse()
str(cars)
## spc_tbl_ [72,983 × 34] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ RefId : num [1:72983] 1 2 3 4 5 6 7 8 9 10 ...
## $ IsBadBuy : num [1:72983] 0 0 0 0 0 0 0 0 0 0 ...
## $ PurchDate : chr [1:72983] "12/7/2009" "12/7/2009" "12/7/2009" "12/7/2009" ...
## $ Auction : chr [1:72983] "ADESA" "ADESA" "ADESA" "ADESA" ...
## $ VehYear : num [1:72983] 2006 2004 2005 2004 2005 ...
## $ VehicleAge : num [1:72983] 3 5 4 5 4 5 5 4 2 2 ...
## $ Make : chr [1:72983] "MAZDA" "DODGE" "DODGE" "DODGE" ...
## $ Model : chr [1:72983] "MAZDA3" "1500 RAM PICKUP 2WD" "STRATUS V6" "NEON" ...
## $ Trim : chr [1:72983] "i" "ST" "SXT" "SXT" ...
## $ SubModel : chr [1:72983] "4D SEDAN I" "QUAD CAB 4.7L SLT" "4D SEDAN SXT FFV" "4D SEDAN" ...
## $ Color : chr [1:72983] "RED" "WHITE" "MAROON" "SILVER" ...
## $ Transmission : chr [1:72983] "AUTO" "AUTO" "AUTO" "AUTO" ...
## $ WheelTypeID : chr [1:72983] "1" "1" "2" "1" ...
## $ WheelType : chr [1:72983] "Alloy" "Alloy" "Covers" "Alloy" ...
## $ VehOdo : num [1:72983] 89046 93593 73807 65617 69367 ...
## $ Nationality : chr [1:72983] "OTHER ASIAN" "AMERICAN" "AMERICAN" "AMERICAN" ...
## $ Size : chr [1:72983] "MEDIUM" "LARGE TRUCK" "MEDIUM" "COMPACT" ...
## $ TopThreeAmericanName : chr [1:72983] "OTHER" "CHRYSLER" "CHRYSLER" "CHRYSLER" ...
## $ MMRAcquisitionAuctionAveragePrice: chr [1:72983] "8155" "6854" "3202" "1893" ...
## $ MMRAcquisitionAuctionCleanPrice : chr [1:72983] "9829" "8383" "4760" "2675" ...
## $ MMRAcquisitionRetailAveragePrice : chr [1:72983] "11636" "10897" "6943" "4658" ...
## $ MMRAcquisitonRetailCleanPrice : chr [1:72983] "13600" "12572" "8457" "5690" ...
## $ MMRCurrentAuctionAveragePrice : chr [1:72983] "7451" "7456" "4035" "1844" ...
## $ MMRCurrentAuctionCleanPrice : chr [1:72983] "8552" "9222" "5557" "2646" ...
## $ MMRCurrentRetailAveragePrice : chr [1:72983] "11597" "11374" "7146" "4375" ...
## $ MMRCurrentRetailCleanPrice : chr [1:72983] "12409" "12791" "8702" "5518" ...
## $ PRIMEUNIT : chr [1:72983] "NULL" "NULL" "NULL" "NULL" ...
## $ AUCGUART : chr [1:72983] "NULL" "NULL" "NULL" "NULL" ...
## $ BYRNO : num [1:72983] 21973 19638 19638 19638 19638 ...
## $ VNZIP1 : num [1:72983] 33619 33619 33619 33619 33619 ...
## $ VNST : chr [1:72983] "FL" "FL" "FL" "FL" ...
## $ VehBCost : num [1:72983] 7100 7600 4900 4100 4000 5600 4200 4500 5600 7700 ...
## $ IsOnlineSale : num [1:72983] 0 0 0 0 0 0 0 0 0 0 ...
## $ WarrantyCost : num [1:72983] 1113 1053 1389 630 1020 ...
## - attr(*, "spec")=
## .. cols(
## .. RefId = col_double(),
## .. IsBadBuy = col_double(),
## .. PurchDate = col_character(),
## .. Auction = col_character(),
## .. VehYear = col_double(),
## .. VehicleAge = col_double(),
## .. Make = col_character(),
## .. Model = col_character(),
## .. Trim = col_character(),
## .. SubModel = col_character(),
## .. Color = col_character(),
## .. Transmission = col_character(),
## .. WheelTypeID = col_character(),
## .. WheelType = col_character(),
## .. VehOdo = col_double(),
## .. Nationality = col_character(),
## .. Size = col_character(),
## .. TopThreeAmericanName = col_character(),
## .. MMRAcquisitionAuctionAveragePrice = col_character(),
## .. MMRAcquisitionAuctionCleanPrice = col_character(),
## .. MMRAcquisitionRetailAveragePrice = col_character(),
## .. MMRAcquisitonRetailCleanPrice = col_character(),
## .. MMRCurrentAuctionAveragePrice = col_character(),
## .. MMRCurrentAuctionCleanPrice = col_character(),
## .. MMRCurrentRetailAveragePrice = col_character(),
## .. MMRCurrentRetailCleanPrice = col_character(),
## .. PRIMEUNIT = col_character(),
## .. AUCGUART = col_character(),
## .. BYRNO = col_double(),
## .. VNZIP1 = col_double(),
## .. VNST = col_character(),
## .. VehBCost = col_double(),
## .. IsOnlineSale = col_double(),
## .. WarrantyCost = col_double()
## .. )
## - attr(*, "problems")=<externalptr>
# $ RefId : num [1:72983] 1 2 3 4 5 6 7 8 9 10 ...
# $ IsBadBuy : num [1:72983] 0 0 0 0 0 0 0 0 0 0 ...
# $ PurchDate : chr [1:72983] "12/7/2009" "12/7/2009"
# OR
# RefId = col_double()
# IsBadBuy = col_double()
# PurchDate = col_character()
8. How many cars (rows) are in the dataset? How many variables (columns) are recorded for each car?
# Look in the environment or..
dim(cars)
## [1] 72983 34
nrow(cars)
## [1] 72983
# There are 72983 cars in the dataset and 34 variables.
9. Filter the dataset to keep only vehicles that cost more than $5000 (“VehBCost”). You should also remove missing values for cost. Replace the original “cars” object by reassigning the new filtered dataset to “cars”. How many vehicles are left after filtering?
Hint: The filter()
function also removes missing values.
cars <- filter(cars, VehBCost > 5000)
nrow(cars)
## [1] 59957
# OR
cars <- cars %>% filter(VehBCost > 5000)
nrow(cars)
## [1] 59957
# There are 59957 cars after filtering by price.
10. From this point on, work with the filtered “cars” dataset from the above question. Given the average car loan today is 70 months, create a new variable (column) called “MonthlyPrice” that shows the monthly cost for each car (Divide “VehBCost” by 70). Check to make sure the new column is there.
Hint: use the mutate()
function.
cars <- cars %>% mutate(MonthlyPrice = VehBCost / 70)
# OR
cars <- mutate(cars, MonthlyPrice = VehBCost / 70)
colnames(cars)
## [1] "RefId" "IsBadBuy"
## [3] "PurchDate" "Auction"
## [5] "VehYear" "VehicleAge"
## [7] "Make" "Model"
## [9] "Trim" "SubModel"
## [11] "Color" "Transmission"
## [13] "WheelTypeID" "WheelType"
## [15] "VehOdo" "Nationality"
## [17] "Size" "TopThreeAmericanName"
## [19] "MMRAcquisitionAuctionAveragePrice" "MMRAcquisitionAuctionCleanPrice"
## [21] "MMRAcquisitionRetailAveragePrice" "MMRAcquisitonRetailCleanPrice"
## [23] "MMRCurrentAuctionAveragePrice" "MMRCurrentAuctionCleanPrice"
## [25] "MMRCurrentRetailAveragePrice" "MMRCurrentRetailCleanPrice"
## [27] "PRIMEUNIT" "AUCGUART"
## [29] "BYRNO" "VNZIP1"
## [31] "VNST" "VehBCost"
## [33] "IsOnlineSale" "WarrantyCost"
## [35] "MonthlyPrice"
11. What is the range of the manufacture year (“VehYear”) of the vehicles?
range(cars %>% pull(VehYear))
## [1] 2001 2010
# OR
car_range <- pull(cars, VehYear)
range(car_range)
## [1] 2001 2010
# OR
range(cars$VehYear)
## [1] 2001 2010
count(cars, VehYear)
## # A tibble: 10 × 2
## VehYear n
## <dbl> <int>
## 1 2001 532
## 2 2002 1741
## 3 2003 3859
## 4 2004 7262
## 5 2005 12296
## 6 2006 15494
## 7 2007 11079
## 8 2008 6871
## 9 2009 822
## 10 2010 1
# The range is 2001 2010.
12. Create a random sample with of mileage (odometer reading) from cars
. To determine the column that corresponds to mileage (The vehicle’s odometer reading), check the “key” corresponding to the data dictionary that you imported above in question 6. Use sample()
and pull()
. Remember that by default random samples differ each time you run the code.
sample(pull(cars, VehOdo), size = 20)
## [1] 85358 44568 72443 62454 74440 78385 52967 52032 68419 81696 28489 79712
## [13] 84830 68899 88274 58726 68750 90469 89339 80996
13. How many cars were from before 2004? What percent/proportion do these represent? Use:
filter()
and nrow()
group_by()
and summarize()
orsum()
# How many cars were from before 2004?
nrow(cars %>% filter(VehYear < 2004))
## [1] 6132
# OR
cars %>%
group_by(VehYear < 2004) %>%
summarize(total = n())
## # A tibble: 2 × 2
## `VehYear < 2004` total
## <lgl> <int>
## 1 FALSE 53825
## 2 TRUE 6132
# OR
sum(cars$VehYear < 2004)
## [1] 6132
# what percent/proportion do these represent?
nrow(cars %>% filter(VehYear < 2004)) / nrow(cars)
## [1] 0.1022733
# OR
mean(cars$VehYear < 2004)
## [1] 0.1022733
# The percent of cars were from before 2004 is 10.22733.
14. How many different vehicle manufacturers/makes (“Make”) are there?
Hint: use length()
with unique()
or table()
. Remember to pull()
the right column.
cars %>% distinct(Make) %>% nrow()
## [1] 32
# OR
cars %>%
pull(Make) %>%
unique() %>%
length()
## [1] 32
# OR
length(unique(cars %>% pull(Make)))
## [1] 32
# OR
length(unique(cars$Make))
## [1] 32
# 32 unique makes.
15. How many different vehicle models (“Model”) are there?
cars %>% distinct(Model) %>% count()
## # A tibble: 1 × 1
## n
## <int>
## 1 985
# OR
cars %>%
pull(Model) %>%
unique() %>%
length()
## [1] 985
# 985 unique models.
16. Group all cars by color (“Color”) and calculate the mean acquisition cost paid for the vehicle at time of purchase?
Hint: Use group_by()
with summarize()
. To determine the column that corresponds to “acquisition cost paid for the vehicle at time of purchase”, check the “key” corresponding to the data dictionary that you imported above in question 6.
cars %>%
group_by(Color) %>%
summarize(mean = mean(VehBCost))
## # A tibble: 17 × 2
## Color mean
## <chr> <dbl>
## 1 BEIGE 7317.
## 2 BLACK 7538.
## 3 BLUE 7182.
## 4 BROWN 7509.
## 5 GOLD 7052.
## 6 GREEN 7089.
## 7 GREY 7551.
## 8 MAROON 7220.
## 9 NOT AVAIL 7151.
## 10 NULL 5860
## 11 ORANGE 7135.
## 12 OTHER 7429.
## 13 PURPLE 6889.
## 14 RED 7279.
## 15 SILVER 7175.
## 16 WHITE 7201.
## 17 YELLOW 6922.
17. Extend on the code you wrote for question 16. Use the arrange()
function to sort the output by mean acquisition cost. What color car has the greatest mean acquisition cost?
cars %>%
group_by(Color) %>%
summarize(mean = mean(VehBCost)) %>%
arrange(desc(mean))
## # A tibble: 17 × 2
## Color mean
## <chr> <dbl>
## 1 GREY 7551.
## 2 BLACK 7538.
## 3 BROWN 7509.
## 4 OTHER 7429.
## 5 BEIGE 7317.
## 6 RED 7279.
## 7 MAROON 7220.
## 8 WHITE 7201.
## 9 BLUE 7182.
## 10 SILVER 7175.
## 11 NOT AVAIL 7151.
## 12 ORANGE 7135.
## 13 GREEN 7089.
## 14 GOLD 7052.
## 15 YELLOW 6922.
## 16 PURPLE 6889.
## 17 NULL 5860
18. How many vehicles were red and have fewer than 30,000 miles? To determine the column that corresponds to mileage (The vehicle’s odometer reading), check the “key” corresponding to the data dictionary that you imported above in question 6. use:
filter()
and count()
orsum()
cars %>%
filter(Color == "RED" & VehOdo < 30000) %>%
count()
## # A tibble: 1 × 1
## n
## <int>
## 1 24
# OR
sum(cars$Color == "RED" & cars$VehOdo < 30000)
## [1] 24
# A total of 24 cars.
19. How many vehicles are blue or red? use:
filter()
and count()
orsum()
cars %>%
filter(Color == "BLUE" | Color == "RED") %>%
count()
## # A tibble: 1 × 1
## n
## <int>
## 1 13777
# OR
cars %>%
filter(Color %in% c("BLUE", "RED")) %>%
count()
## # A tibble: 1 × 1
## n
## <int>
## 1 13777
# OR
sum(pull(cars, Color) %in% c("BLUE", "RED"))
## [1] 13777
# OR
sum(cars$Color == "BLUE" | cars$Color == "RED")
## [1] 13777
# OR
sum(cars$Color %in% c("BLUE", "RED"))
## [1] 13777
# A total of 13777 cars.
20. Select all columns in “cars” where the column names starts with “Veh” (using select()
and starts_with()
. How many columns are there?
cars %>%
select(starts_with("Veh"))
## # A tibble: 59,957 × 4
## VehYear VehicleAge VehOdo VehBCost
## <dbl> <dbl> <dbl> <dbl>
## 1 2006 3 89046 7100
## 2 2004 5 93593 7600
## 3 2004 5 81054 5600
## 4 2007 2 49921 5600
## 5 2007 2 84872 7700
## 6 2005 4 80080 5500
## 7 2001 8 75419 5300
## 8 2005 4 79315 5400
## 9 2005 4 71254 7800
## 10 2006 3 74722 6900
## # ℹ 59,947 more rows
# 4 columns.
The following questions are not required for full credit, but can make up for any points lost on other questions.
A. Using “cars”, create a new binary (TRUEs and FALSEs) column to indicate if the car has an automatic transmission. Call the new column “is_automatic”.
cars <- cars %>% mutate(is_automatic = (Transmission == "AUTO"))
B. What is the average vehicle odometer reading for cars that are both RED and NISSANs? How does this compare with vehicles that do NOT fit this criteria?
mean_red_nissan <- cars %>%
filter(Color == "RED" & Make == "NISSAN") %>%
summarize(mean = mean(VehOdo)) %>%
pull()
mean_not_red_nissan <- cars %>%
filter(Color != "RED" | Make != "NISSAN") %>%
summarize(mean = mean(VehOdo)) %>%
pull()
# Red Nissans have an average odometer of 75117.32 while cars not fitting this criteria are 70324.34.
C. Among red Nissans, what is the distribution of vehicle ages? Use count()
.
red_nissan <- cars %>% filter(Color == "RED" & Make == "NISSAN")
red_nissan %>%
group_by(VehicleAge) %>%
count()
## # A tibble: 8 × 2
## # Groups: VehicleAge [8]
## VehicleAge n
## <dbl> <int>
## 1 2 8
## 2 3 35
## 3 4 48
## 4 5 34
## 5 6 13
## 6 7 14
## 7 8 4
## 8 9 2
D. How many vehicles (using filter()
or sum()
) are made by Chrysler or Nissan and are white or silver?
cars %>%
filter(Make %in% c("CHRYSLER", "NISSAN") & Color %in% c("WHITE", "SILVER")) %>%
count()
## # A tibble: 1 × 1
## n
## <int>
## 1 3718
# A total of 3718 cars.
E. Select all columns in “cars” where the column names starts with “Veh” or “MMR” (using select()
and starts_with()
. How many columns are there?
cars %>%
select(starts_with(c("Veh", "MMR")))
## # A tibble: 59,957 × 12
## VehYear VehicleAge VehOdo VehBCost MMRAcquisitionAuctionAveragePrice
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 2006 3 89046 7100 8155
## 2 2004 5 93593 7600 6854
## 3 2004 5 81054 5600 3901
## 4 2007 2 49921 5600 6196
## 5 2007 2 84872 7700 7845
## 6 2005 4 80080 5500 5243
## 7 2001 8 75419 5300 3168
## 8 2005 4 79315 5400 4225
## 9 2005 4 71254 7800 5684
## 10 2006 3 74722 6900 6999
## # ℹ 59,947 more rows
## # ℹ 7 more variables: MMRAcquisitionAuctionCleanPrice <chr>,
## # MMRAcquisitionRetailAveragePrice <chr>,
## # MMRAcquisitonRetailCleanPrice <chr>, MMRCurrentAuctionAveragePrice <chr>,
## # MMRCurrentAuctionCleanPrice <chr>, MMRCurrentRetailAveragePrice <chr>,
## # MMRCurrentRetailCleanPrice <chr>
# OR
cars %>%
select(starts_with("Veh") | starts_with("MMR"))
## # A tibble: 59,957 × 12
## VehYear VehicleAge VehOdo VehBCost MMRAcquisitionAuctionAveragePrice
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 2006 3 89046 7100 8155
## 2 2004 5 93593 7600 6854
## 3 2004 5 81054 5600 3901
## 4 2007 2 49921 5600 6196
## 5 2007 2 84872 7700 7845
## 6 2005 4 80080 5500 5243
## 7 2001 8 75419 5300 3168
## 8 2005 4 79315 5400 4225
## 9 2005 4 71254 7800 5684
## 10 2006 3 74722 6900 6999
## # ℹ 59,947 more rows
## # ℹ 7 more variables: MMRAcquisitionAuctionCleanPrice <chr>,
## # MMRAcquisitionRetailAveragePrice <chr>,
## # MMRAcquisitonRetailCleanPrice <chr>, MMRCurrentAuctionAveragePrice <chr>,
## # MMRCurrentAuctionCleanPrice <chr>, MMRCurrentRetailAveragePrice <chr>,
## # MMRCurrentRetailCleanPrice <chr>
# 12 columns.
F. Knit your document into a report.
You use the knit button to do this. Make sure all your code is working first!