library(tidyverse)
library(ggridges)
Quarto
First, let’s make sure you know how to use Markdown formatting to style a Quarto document.
Make this text bold.
Make this text italicized.
Make these into a bullet point list:
Apples
Bananas
Potatoes
Edit the YAML to remove warning messages from being output in the rendered HTML file
Using code chunk options, make it so this chunk shows the plot but not the source code:
- Using code chunk options, remove the messages about bandwidth
geom_density_ridges()
chose to use:
ggplot(data = mpg,
mapping = aes(y = manufacturer, x = hwy)) +
geom_density_ridges() +
labs(x = "",
y = "",
title = "Highway Milage (mpg) for Different Car Manufacturers"
)
- Using code chunk options, make it so that these plots are printed side-by-side:
ggplot(data = mpg,
mapping = aes(y = manufacturer, x = hwy)) +
geom_boxplot() +
labs(x = "",
y = "",
title = "Highway Milage (mpg) for Different Car Manufacturers"
)ggplot(data = mpg,
mapping = aes(y = manufacturer, x = hwy)) +
geom_density_ridges() +
labs(x = "",
y = "",
title = "Highway Milage (mpg) for Different Car Manufacturers"
)
- Using code chunk options, make it so this chunk shows the code but not the output:
2 + 2
- Using code chunk options, make it so the file can still knit even though this chunk has an error
2 + a
- Using code chunk options, create a descriptive
label
for each of the code chunks above.
Data Wrangling Review
Since you already seen some ggplot
s, let’s do a bit of review on data handling. In this class, we will exclusively make use of tools from the tidyverse
suite of packages to perform our data cleaning and wrangling operations. If you are less familiar with these packages or it’s been some time since you used them, I would strongly recommend referencing the function documentation!
For these problems, we will continue to work with the mpg
data frame, making various changes to the data to clean it up.
- The
fl
variable describes the type of fuel for each car, with levels:p
,r
,e
,d
, andc
. Do some research into what each of these labels mean! Then, use theif_else()
function to create a new variable (fuel_type
) with two levels:petrol
(any car using petroleum-based gas) andalternative energy
(any car not using petroleum-based gas).
P: premium, R: regular, E: electric, D: diesel, C: Compressed Natural Gas (CNG) (no clear official documentation, this could be incorrect)
<- mpg %>%
mpg mutate(fuel_type = if_else(fl == "c"|fl=="e", "alternative energy","petrol"))
- The
drv
variable describes if the car has front drive (f
), rear drive (r
), or four wheel drive (4
). Let’s make better labels for these values! Specifically, use thecase_when()
function to change thedrv
variable to have the following levels:front
,rear
,four wheel
.
<- mpg %>%
mpg mutate(drv = case_when(
== "f"~"front",
drv == "r"~"rear",
drv == "4"~"four wheel"
drv ))
13. The trans
variable contains two pieces of information, (1) the transmission style (auto
or manual
) and the specific type of transmission (e.g., l5
, m5
). Using the str_split()
function, create a new variable (trans_type
) containing the specific type of transmission of each car. Once you’ve made this new variable, use the rename()
function to change the name of the trans
column to trans_style
.
Hint: You will need to deal with the stray parenthesis! (string split + remove extra “)” )
<- mpg %>%
mpg mutate(trans_type = str_split(trans, "\\(", simplify = T)[,2],
trans_type = str_remove(trans_type,"\\)"),
trans = str_split(trans, "\\(", simplify = T)[,1]
%>%
) rename(trans_style=trans)
Getting to know your classmates
Find someone who took Stat 331 from a different professor than you. Compare your experiences. Tell me their name and professor. List one or two things that you think you learned more about, and one or two things that they learned more about.
Jacob (the GOAT) took STAT 331 with Dr. Theobold.
Jacob learned more about
stringr
. I did more work withdplyr
andtidyverse
as this was the main focus of the course.Find someone in the class who does not share your birth month. Tell me their name and birthday, and use R to find out how many days apart your birthdays are.
Rachel’s birthday is August 14, 2002. Mine is July 11, 2002.
<- as.Date("2002-07-11") start_date <- as.Date("2002-08-14") end_date <- end_date - start_date num_days num_days
Time difference of 34 days