BCB744 Bonus Task

Assessment Sheet

12–14. Tidy Data

Question 1

What are the key principles of tidy data? (/3)

Answer

  • ✓ Each variable forms a column.
  • ✓ Each observation forms a row.
  • ✓ Each type of observational unit forms a table.

Question 2

Using the untidy data (SACTN2) and the tidy data (SACTN2_tidy), create line graphs, one for each of DEA, SAWS, and KZNSB, showing a time series of temperature. Ensure you have a column of three figures (ncol = 1). Use the fewest number of lines of code possible. You should end up with two graphs, each with three panels. (/13)

Answer

library(tidyverse)

load("../data/SACTN_mangled.RData") # ✓

SACTN2_tidy <- pivot_longer(SACTN2, cols = c("DEA", "KZNSB", "SAWS"),
                            names_to = "src",
                            values_to = "temp") # ✓

# Starting with SACTN2: one could be sneaky and cheat by using
# 'pivot_wider()' in the pipeline
SACTN2 |>  # ✓ x 6
  pivot_longer(cols = c("DEA", "KZNSB", "SAWS"),
               names_to = "src",
               values_to = "temp") |> 
  ggplot(aes(x = date, y = temp)) +
  geom_line(aes(col = site, linetype = type)) +
  facet_wrap(~ src, ncol = 1) +
  labs(title = "Untidy Data", # ✓
       x =  "Date", y = "Temperature (°C)")

# For the untidy data, above, I'll also allocate marks if you insisted in 
# creating more work for yourself by doing it the long way.... ( # ✓ x 7)

# Starting with SACTN2_tidy
# (creates an identical plot)
ggplot(data = SACTN2_tidy, aes(x = date, y = temp)) +  # ✓ x 4
  geom_line(aes(col = site, linetype = type)) +
  facet_wrap(~ src, ncol = 1) +
  labs(title = "Tidy Data", # ✓
       x =  "Date", y = "Temperature (°C)")

Reuse

Citation

BibTeX citation:
@online{smit,_a._j.,
  author = {Smit, A. J.,},
  title = {BCB744 {Bonus} {Task}},
  url = {http://tangledbank.netlify.app/assessments/BCB744_Task_D.html},
  langid = {en}
}
For attribution, please cite this work as:
Smit, A. J. BCB744 Bonus Task. http://tangledbank.netlify.app/assessments/BCB744_Task_D.html.