Import your data

data <- read.csv("../00_data/myData.csv")
set.seed(1234)
depart_small <- sample_n(data, 5) %>% select(sort_name, album, genre)
depart_small
##             sort_name                      album
## 1     Mott the Hoople        All the Young Dudes
## 2           Yardbirds           Having a Rave Up
## 3 Morrissette, Alanis         Jagged Little Pill
## 4      Williams, Hank The Complete Hank Williams
## 5               Clash                  The Clash
##                               genre
## 1                              <NA>
## 2                  Blues/Blues Rock
## 3            Indie/Alternative Rock
## 4                              <NA>
## 5 Punk/Post-Punk/New Wave/Power Pop

Pivoting

depart_wide <- depart_small %>%
    
    pivot_wider(names_from = sort_name, 
                values_from = genre)

depart_wide
## # A tibble: 5 × 6
##   album `Mott the Hoople` Yardbirds `Morrissette, Alanis` `Williams, Hank` Clash
##   <chr> <chr>             <chr>     <chr>                 <chr>            <chr>
## 1 All … <NA>              <NA>      <NA>                  <NA>             <NA> 
## 2 Havi… <NA>              Blues/Bl… <NA>                  <NA>             <NA> 
## 3 Jagg… <NA>              <NA>      Indie/Alternative Ro… <NA>             <NA> 
## 4 The … <NA>              <NA>      <NA>                  <NA>             <NA> 
## 5 The … <NA>              <NA>      <NA>                  <NA>             Punk…

long to wide form

set.seed(1234)
depart_small <- sample_n(data, 5) %>% select(sort_name, album, weeks_on_billboard)
depart_small
##             sort_name                      album weeks_on_billboard
## 1     Mott the Hoople        All the Young Dudes                 19
## 2           Yardbirds           Having a Rave Up                 33
## 3 Morrissette, Alanis         Jagged Little Pill                113
## 4      Williams, Hank The Complete Hank Williams                 NA
## 5               Clash                  The Clash                  6
depart_wide <- depart_small %>%
    
    pivot_wider(names_from = sort_name, 
                values_from = weeks_on_billboard)

depart_wide
## # A tibble: 5 × 6
##   album `Mott the Hoople` Yardbirds `Morrissette, Alanis` `Williams, Hank` Clash
##   <chr>             <int>     <int>                 <int>            <int> <int>
## 1 All …                19        NA                    NA               NA    NA
## 2 Havi…                NA        33                    NA               NA    NA
## 3 Jagg…                NA        NA                   113               NA    NA
## 4 The …                NA        NA                    NA               NA    NA
## 5 The …                NA        NA                    NA               NA     6

wide to long form

depart_wide %>%
    
    pivot_longer(`Mott the Hoople`:Clash, 
                 values_drop_na = TRUE)
## # A tibble: 4 × 3
##   album               name                value
##   <chr>               <chr>               <int>
## 1 All the Young Dudes Mott the Hoople        19
## 2 Having a Rave Up    Yardbirds              33
## 3 Jagged Little Pill  Morrissette, Alanis   113
## 4 The Clash           Clash                   6

Separating and Uniting

Separate a column

depart_small %>%
    
    separate(col = album, into = c("first_name", "Middle_inital", "last_name"))
## Warning: Expected 3 pieces. Additional pieces discarded in 3 rows [1, 2, 4].
## Warning: Expected 3 pieces. Missing pieces filled with `NA` in 1 rows [5].
##             sort_name first_name Middle_inital last_name weeks_on_billboard
## 1     Mott the Hoople        All           the     Young                 19
## 2           Yardbirds     Having             a      Rave                 33
## 3 Morrissette, Alanis     Jagged        Little      Pill                113
## 4      Williams, Hank        The      Complete      Hank                 NA
## 5               Clash        The         Clash      <NA>                  6

Unite two columns

depart_small %>%
    
    unite(col = "rate", c(sort_name,album), sep = "/", )
##                                        rate weeks_on_billboard
## 1       Mott the Hoople/All the Young Dudes                 19
## 2                Yardbirds/Having a Rave Up                 33
## 3    Morrissette, Alanis/Jagged Little Pill                113
## 4 Williams, Hank/The Complete Hank Williams                 NA
## 5                           Clash/The Clash                  6

Missing Values