Import your data

disney <- read_csv("../00_data/disney_films.csv")
## Rows: 27 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): film, release_date, film_rating
## dbl (2): number, run_time
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Pivoting

long to wide form

disney_longer <- disney %>%
    
    pivot_longer(cols = c(`film`, `film_rating`),
                 names_to = "movie",
                 values_to = "rating")

wide to long form

disney_longer %>%
    
    pivot_wider(names_from = movie,
                values_from = rating)
## # A tibble: 27 × 5
##    number release_date run_time film            film_rating
##     <dbl> <chr>           <dbl> <chr>           <chr>      
##  1      1 11/22/1995         81 Toy Story       G          
##  2      2 11/25/1998         95 A Bug's Life    G          
##  3      3 11/24/1999         92 Toy Story 2     G          
##  4      4 11/2/2001          92 Monsters, Inc.  G          
##  5      5 5/30/2003         100 Finding Nemo    G          
##  6      6 11/5/2004         115 The Incredibles PG         
##  7      7 6/9/2006          117 Cars            G          
##  8      8 6/29/2007         111 Ratatouille     G          
##  9      9 6/27/2008          98 WALL-E          G          
## 10     10 5/29/2009          96 Up              PG         
## # ℹ 17 more rows

Separating and Uniting

Separate a column

disney_sep <- disney_longer %>%
    
    separate(col = number, into = c("movie"))

Unite two columns

disney_sep %>%
    
    unite(col = "film", run_time:rating, sep = "/")
## # A tibble: 54 × 3
##    movie release_date film             
##    <chr> <chr>        <chr>            
##  1 1     11/22/1995   81/Toy Story     
##  2 1     11/22/1995   81/G             
##  3 2     11/25/1998   95/A Bug's Life  
##  4 2     11/25/1998   95/G             
##  5 3     11/24/1999   92/Toy Story 2   
##  6 3     11/24/1999   92/G             
##  7 4     11/2/2001    92/Monsters, Inc.
##  8 4     11/2/2001    92/G             
##  9 5     5/30/2003    100/Finding Nemo 
## 10 5     5/30/2003    100/G            
## # ℹ 44 more rows

Missing Values

disney <- tibble(
    film = c("Soul", "Luca", "Turning Red", "Light Year"),
    run_time = c(100, 151, NA, NA),
    film_rating = c("PG", NA, NA, NA)
)
disney %>%
    
    pivot_wider(names_from = film, values_from = run_time)
## # A tibble: 2 × 5
##   film_rating  Soul  Luca `Turning Red` `Light Year`
##   <chr>       <dbl> <dbl>         <dbl>        <dbl>
## 1 PG            100    NA            NA           NA
## 2 <NA>           NA   151            NA           NA