ETC5513: Collaborative and Reproducible Practices
Tutorial 2
🎯 Objectives
- Working on a reproducible RStudio Project
- Working on a HTML report and example different YAML themes
- Practice Markdown syntax
- Practice R
- Practice R chunk options
- Gain experience on data wrangling using the
tidyverse
suite of packages - Producing exploratory data analysis figures using the package
ggplot2
- Learn how to add figure captions
- Create HTML tables and learn how to add captions
Exercise 1: Hands on practice with COVID-19 Data
- The data for the tutorial is inside a folder called Data, which is bundled with the RStudio Project. Find that file in the lower right pane where all your files are listed.
- Create a new section heading in your
qmd
document to read the data with the title “Reading Coronavirus Data”
Hint: Use#
- Inside this new section, create an R Code Chunk with options
echo: true
,warning: false
,message: false
called “Reading data” and insert the following code:
<- read_csv("Data/COVID19_March12_Hubei.csv") dat
- Insert a new R Chunk and find out what information you can get from the command
head(dat)
- Modify the
head
command to display 10 rows. - Create another two R chunks and use in each of them the R functions
glimpse()
andstr()
. What information can you get from those commands? Hint: For more information on R functions, type in the R conolse?glimpse()
. - Using an R inline command, write the dimension of the dataset in a sentence.
Hint: Have a look atncol
andnrow
. - Add a new subsection heading (
###
) with “Why is it important to know the dimension of your dataset?” and write a brief sentence with the explanation - Add a new subsection heading (
###
) with “What are the variable names in the dataset?” and display the names of the dataset variables using R.
Hint:?names()
in the R consolee - Select two variables and use a markdown list to briefly explain what each of the variables are measuring.
Exercise 4: COVID19 Data Wrangling
- Using the R package
dplyr
(which is loaded withtidyverse
), and using the pipe (|>
), create a new dataset calleddata_cleaned
that only contains the following variables:country
age
sex
city
province
latitude
longitude
- Inspect
data_cleaned
and describe using a markdown list the type of variables in this new dataset. Write the names of the variables in bold. Do you think the variable attributes are correct?
- Convert the variable
age
into a numeric vector
- Inspect the first 20 values of
age
. What do you observe? What is the proportion of missing values in the variableage
? Make sure you round the results to two decimal numbers.
- Remove cases for which we don’t have information on the person’s age and keep cases for which the gender of the patient is known. Give this new data set the name
data_filtered
.
- What is the dimension of this new data set? Compare it with the dimension of
cleaned_data
. How many cases have we lost?
- Examine the variable age using the function
summary()
. Do you see any problems in the data?
- Remove patient entries with age below 1. You can save this back into
data_filtered
.
- Provide a table summary of the variable age using the
kable()
function from thekableExtra
package. Give it the caption “COVID-19 Age Summary”.
- Visualize the age distribution using a histogram and give an explanation about the information that a histogram convey. In addition change the x label in the plot to Age and remove the y axis label.
Hint: You’ll need to useggplot2
for this.
- Change the color of the histogram using
geom_histogram(color = "blue", fill = "white")
- Visualize the age distribution for females and males and add the following caption to the figure “Age frequencies of COVID19 patients in China per gender”
(Hint:facet_wrap()
)
- Count the number of cases per province and display a table of the top 10 provinces. Store results into an object called
cases_by_province
.
Hint: Replace XXX by the adequate variable names in the code below.
```{r}
cases_by_province <- data_filtered |>
select(XXX) |>
filter(!is.na(XXX)) |>
group_by(province) |>
summarise(cases = n())|>
arrange(-XXX)
```
- Recreate this table using the below code:
```{r}
cases_by_province_alternative <- data_filtered |>
filter(!is.na(province)) |>
count(province, name="cases", sort=TRUE)
```