# This script follows loosely chapter 5 of the book "R for Data Science" (2nd edition)
# DATA TIDYING

library(tidyverse)

############## Tidy data ###############

# These 3 tables show the same values of four variables: contry, year, population and 
# number of documented cases of tuberculosis, but each one in a different way.

table1

table2

table3

# Only table1 is "tidy", because:
# - Each variable is a column; each column is a variable.
# - Each observation is a row; each row is an observation.
# - Each value is a cell; each cell is a single value.

# Table 1 is easy to work with. Let us compute the rate of tubercusosis diagnosis
# for each 10000
table1 %>% mutate(rate = cases / population * 10000)

# Compute total cases per year
table1 %>% group_by(year) %>% summarize(total_cases = sum(cases))

# Visualize changes over time
ggplot(table1, aes(x = year, y = cases)) +
  geom_line(aes(group = country), color = "grey50") +
  geom_point(aes(color = country, shape = country)) +
  scale_x_continuous(breaks = c(1999, 2000))


################# Lengthening data ###################

# Example of "pivoting" the data into a tidy form - the billboard dataset records
# the billboard rank of songs in the year 2000:
billboard

# To tidy this data, we will use pivot_longer():
billboard %>% 
  pivot_longer(
    cols = starts_with("wk"), # columns that are not variables
    names_to = "week",        # names the variable stored in the column names
    values_to = "rank"        # names the variable stored in the cell values
  )

# There are some NA's, which correspond to songs that were in the top 100 for less
# than 76 weeks (the maximum weeks in the top 100 for any of these songs).
# We'll get rid of these lines:
billboard %>%  
  pivot_longer(
    cols = starts_with("wk"), 
    names_to = "week", 
    values_to = "rank",
    values_drop_na = TRUE
  )

# The data is now tidy, but we will also convert values of week to numbers using
# parse_number():
billboard_longer <- billboard %>% 
  pivot_longer(
    cols = starts_with("wk"), 
    names_to = "week", 
    values_to = "rank",
    values_drop_na = TRUE
  )  %>%  
  mutate(
    week = parse_number(week)
  )
billboard_longer

# Now let's visualize how song ranks vary over time:
billboard_longer %>%  
  ggplot(aes(x = week, y = rank, group = track)) + 
  geom_line(alpha = 0.25) + 
  scale_y_reverse()
