Example Assignment R Solutions
library(knitr)
library(kableExtra)
library(pillar)
formatted_table <- function(df) {
col_types <- sapply(df, pillar::type_sum)
new_col_names <- paste0(names(df), "<br>", "<span style='font-weight: normal;'>", col_types, "</span>")
kbl(df,
col.names = new_col_names,
escape = FALSE, # This is crucial to allow the <br> tag to work
format = "html" # Ensure HTML format, although often auto-detected
) %>%
kable_styling(bootstrap_options = c("striped", "hover", "responsive"))
}Exercise 01
The Comet Goldfish Lifespan Prediction Dataset provides a structured collection of variables used to estimate the longevity of Carassius auratus.
The fictive dataset includes several measurements: - Environmental Factors: Habitat type (e.g., lakes, ponds), water pH, and tank specifications. - Biological Metrics: Average length and weight, gender, and specimen color variations. - Target Variable: Lifespan (years). Answer the following questions:
- Download the data at Predict lifespan of a comet goldfish. Load the csv in RStudio.
- Filter data to exclude blanks for the column Gender. Also create a new column based on the following: True should be mapped to Male, False to Female.
- Create a Pie chart to show the proportion of the different genders. You may need to use R formulas to prepare the data. Provide a logical title.
- Create a box plot to compare the life span between the habitats. Provide a logical title together with axis titles and units.
- Create a barplot that shows the average lifespan for each habitat.
Provide a logical title together with axis titles and units.
Hint: you needgroup_byandsummarisefor this.
- Investigate a possible correlation between the pH of the water and the life span. Use a suitable chart type to visualize this. Provide a logical title together with axis titles and units. Is there a correlation?
Exercise 2
This time we will analyse some weather data from the KNMI. Browse to KNMI - Uurgegevens van het weer in Nederland. Download a dataset from 1951-1960 for the weather station at Eelde.
- Load the data using RStudio. Note: you need to use some tweaks to load the data.
- Find the lowest temperature and highest temperature (at 1.5 m height) using formulas in degrees Celsius (note: read the column info in the text file)!
- At what dates were these temperatures observed? Use formulas to get the answer.
- A tropical temperature is defined as a temperature of 30 °C or higher. How many tropical temperatures are in the dataset (at 1.5 m height)?
- There could be multiple tropical temperatures on the same day. How many tropical days are within the data set (at 1.5 m height)? Hint: use group_by and summarize for this.
- What was the average temperature in 1951? Use formulas to achieve this.
Exercise 3
Use the dataset from Our
World in Data
Download vaccinations.csv.
This dataset contains daily vaccination figures per country worldwide,
with columns such as date, location, total_vaccinations,
people_vaccinated, people_fully_vaccinated, daily_vaccinations, etc.
- Load the data In RStudio.
- Create a chart showing the progression of people_fully_vaccinated_per_hundred (in other words, the percentage) over time for the Netherlands. Filter the data accordingly. Provide a logical title together with axis titles and units.
- Provide a table with the max people_fully_vaccinated_per_hundred per country. Note that the numbers above 100 are in the dataset because of various reasons (e.g. boosters counted as full vaccination, some small countries (like Gibraltar or San Marino) vaccinated people who are not residents etc.).
The end…
This web page is distributed under the terms of the Creative Commons Attribution License which permits unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited. Creative Commons License: CC BY-SA 4.0.