Tidy Data

Pivoting

long to wide form

table4a_long <- table4a %>%
    pivot_longer(cols = c(`1999`,`2000`), names_to = "year", values_to = "cases")

wide to long

table4a_long %>%
    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

Seperating & Uniting

separate a column

table3_sep <- table3 %>%
    separate(col = rate,into = c("cases","population"))

unite two Columns

table3_sep %>%
    unite(col = "rate",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

stocks <- tibble(
  year   = c(2015, 2015, 2015, 2015, 2016, 2016, 2016),
  qtr    = c(   1,    2,    3,    4,    2,    3,    4),
  return = c(1.88, 0.59, 0.35,   NA, 0.92, 0.17, 2.66)
)
stocks %>%
    pivot_wider(names_from = year, values_from = return)
## # A tibble: 4 × 3
##     qtr `2015` `2016`
##   <dbl>  <dbl>  <dbl>
## 1     1   1.88  NA   
## 2     2   0.59   0.92
## 3     3   0.35   0.17
## 4     4  NA      2.66
bikes <- tibble(
  bike_modle   = c("A", "A","B","C", "D"),
  matirial    = c("steel","aliminium", "steel","titanium","carbon fiber"),
  price = c(100,200,300,400,500)
)
bikes %>%
    pivot_wider(names_from = bike_modle,values_from = price)
## # A tibble: 4 × 5
##   matirial         A     B     C     D
##   <chr>        <dbl> <dbl> <dbl> <dbl>
## 1 steel          100   300    NA    NA
## 2 aliminium      200    NA    NA    NA
## 3 titanium        NA    NA   400    NA
## 4 carbon fiber    NA    NA    NA   500
bikes %>%
    complete(bike_modle,matirial)
## # A tibble: 16 × 3
##    bike_modle matirial     price
##    <chr>      <chr>        <dbl>
##  1 A          aliminium      200
##  2 A          carbon fiber    NA
##  3 A          steel          100
##  4 A          titanium        NA
##  5 B          aliminium       NA
##  6 B          carbon fiber    NA
##  7 B          steel          300
##  8 B          titanium        NA
##  9 C          aliminium       NA
## 10 C          carbon fiber    NA
## 11 C          steel           NA
## 12 C          titanium       400
## 13 D          aliminium       NA
## 14 D          carbon fiber   500
## 15 D          steel           NA
## 16 D          titanium        NA
treatment <- tribble(
  ~ person,           ~ treatment, ~response,
  "Derrick Whitmore", 1,           7,
  NA,                 2,           10,
  NA,                 3,           9,
  "Katherine Burke",  1,           4
)

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

Nontidy Data