knitr::opts_chunk$set(echo = TRUE)

# Load package]
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Tidy data

Pivoting

Long to wide form

table4a_longg <- table4a %>%
    
    pivot_longer(cols = c('1999', '2000'),
                 names_to = "year",
                 values_to = "cases")
table4a_longg
## # A tibble: 6 × 3
##   country     year   cases
##   <chr>       <chr>  <dbl>
## 1 Afghanistan 1999     745
## 2 Afghanistan 2000    2666
## 3 Brazil      1999   37737
## 4 Brazil      2000   80488
## 5 China       1999  212258
## 6 China       2000  213766

Wide to long form

table4a_longg %>%
    
    pivot_wider(names_from = year,
                values_from = cases)
## # A tibble: 3 × 3
##   country     `1999` `2000`
##   <chr>        <dbl>  <dbl>
## 1 Afghanistan    745   2666
## 2 Brazil       37737  80488
## 3 China       212258 213766

Separating and Uniting

table3_sep <- table3 %>%
    
    separate(col = rate, into = c("cases", "population"))
table3_sep
## # A tibble: 6 × 4
##   country      year cases  population
##   <chr>       <dbl> <chr>  <chr>     
## 1 Afghanistan  1999 745    19987071  
## 2 Afghanistan  2000 2666   20595360  
## 3 Brazil       1999 37737  172006362 
## 4 Brazil       2000 80488  174504898 
## 5 China        1999 212258 1272915272
## 6 China        2000 213766 1280428583

Unite two columns

table3_sep %>%
    
    unite(col = "rate", c(cases,population), sep = "/")
## # A tibble: 6 × 3
##   country      year rate             
##   <chr>       <dbl> <chr>            
## 1 Afghanistan  1999 745/19987071     
## 2 Afghanistan  2000 2666/20595360    
## 3 Brazil       1999 37737/172006362  
## 4 Brazil       2000 80488/174504898  
## 5 China        1999 212258/1272915272
## 6 China        2000 213766/1280428583

Missing Values

bikes <- tibble(
    bike_model = c("A", "A", "B", "B", "C"),
    material = c("steel", "aluminum", "steel", "aluminum", "steel"),
    price = c(100, 200, 300, 400, 500)
)
bikes %>%
    
    pivot_wider(names_from = bike_model, values_from = price)
## # A tibble: 2 × 4
##   material     A     B     C
##   <chr>    <dbl> <dbl> <dbl>
## 1 steel      100   300   500
## 2 aluminum   200   400    NA
bikes %>%
    
    complete(bike_model, material)
## # A tibble: 6 × 3
##   bike_model material price
##   <chr>      <chr>    <dbl>
## 1 A          aluminum   200
## 2 A          steel      100
## 3 B          aluminum   400
## 4 B          steel      300
## 5 C          aluminum    NA
## 6 C          steel      500
treatment <- tribble(
    ~ person, ~ treatment, ~ response,
    "Derrick Whitmore", 1, 7,
    NA, 2, 10,
    NA, 3, 9,
    "Katherine Burke", 1, 4
)

treatment %>%
    
    fill(person, .direction = "down")
## # A tibble: 4 × 3
##   person           treatment response
##   <chr>                <dbl>    <dbl>
## 1 Derrick Whitmore         1        7
## 2 Derrick Whitmore         2       10
## 3 Derrick Whitmore         3        9
## 4 Katherine Burke          1        4

Non-tidy data