library(tidyverse)
library(edr)

4.2. Using tidyr to Tidy Our Tables

Listing 4.1. The winniweather dataset, printed to the console.

winniweather

Listing 4.2. How to make data pivot_longer() and thus make it more tidy.

winni_mod <- 
  winniweather %>%
  pivot_longer(
    starts_with("temp"),
    names_to = "hour",
    values_to = "temp"
  )

winni_mod

Listing 4.3. Modifying values in the hour column with mutate().

winni_mod <- 
  winni_mod %>%
  mutate(hour = case_when(
    hour == "temp00_00" ~ 0L,
    hour == "temp06_00" ~ 6L,
    hour == "temp12_00" ~ 12L,
    hour == "temp18_00" ~ 18L
  ))

winni_mod

Listing 4.4. Separating the yearmonth column into year and month columns.

winni_tidy <- 
  winni_mod %>%
  separate(
    col = yearmonth,
    into = c("year", "month"),
    sep = "-",
    convert = TRUE
  )

winni_tidy

Listing 4.5. Using arrange() to put the observations in the correct order.

winni_tidy <- 
  winni_tidy %>%
  arrange(year, month, day, hour)

winni_tidy

Listing 4.6. A date-time column is necessary for ggplot time-series plots.

winni_tidy <- 
  winni_tidy %>%
  mutate(iso_date = ISOdate(year, month, day, hour))

winni_tidy

Listing 4.7. A scatterplot of all observations in winni_tidy using ggplot.

ggplot(data = winni_tidy) +
  geom_point(aes(x = iso_date, y = temp))

Listing 4.8. Replacement of missing values with the mutate() and na_if() functions.

winni_tidy <-
  winni_tidy %>%
  mutate(temp = na_if(temp, 9999))

Listing 4.9. Inspecting the data in a column to verify that the mutation had occurred.

winni_tidy %>% 
  select(temp) %>%
  summary()

Listing 4.10. An updated scatterplot of all observations in winni_tidy after encoding a missing value.

ggplot(data = winni_tidy) +
  geom_point(aes(x = iso_date, y = temp))

Listing 4.11. Faceting the temperatures in winni_tidy by the time of day.

ggplot(data = winni_tidy) +
  geom_point(aes(x = iso_date, y = temp)) +
  facet_wrap(vars(hour), labeller = label_both)


rich-iannone/rwr documentation built on Jan. 22, 2021, 7:51 p.m.