Import your data

nhl_rosters <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2024/2024-01-09/nhl_rosters.csv')
## Rows: 54883 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (10): team_code, position_type, headshot, first_name, last_name, positi...
## dbl   (7): season, player_id, sweater_number, height_in_inches, weight_in_po...
## date  (1): birth_date
## 
## ℹ 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.

Make my data small

small_nhl_rosters <- nhl_rosters %>% select(team_code, sweater_number, player_id, first_name) %>% sample_n(10)
small_nhl_rosters
## # A tibble: 10 × 4
##    team_code sweater_number player_id first_name
##    <chr>              <dbl>     <dbl> <chr>     
##  1 COL                   20   8456745 Keith     
##  2 DET                    3   8447875 Bill      
##  3 CGY                   24   8470201 Jiri      
##  4 ANA                   24   8464962 Ruslan    
##  5 BOS                   57   8456556 Steve     
##  6 NYR                   16   8447790 Pat       
##  7 PIT                   25   8450555 Thomas    
##  8 CLR                   31   8450468 Michel    
##  9 SEN                   15   8445135 George    
## 10 NYR                   22   8445028 Jimmy

Pivoting

long to wide form

long_nhl_roster <- small_nhl_rosters %>%
    
pivot_longer(cols = c("player_id", "sweater_number"),
                 names_to  = "player_id",
                 values_to = "sweater_number")

wide to long form

long_nhl_roster %>%
    
    pivot_wider(names_from = player_id,
                values_from = sweater_number)
## # A tibble: 10 × 4
##    team_code first_name player_id sweater_number
##    <chr>     <chr>          <dbl>          <dbl>
##  1 COL       Keith        8456745             20
##  2 DET       Bill         8447875              3
##  3 CGY       Jiri         8470201             24
##  4 ANA       Ruslan       8464962             24
##  5 BOS       Steve        8456556             57
##  6 NYR       Pat          8447790             16
##  7 PIT       Thomas       8450555             25
##  8 CLR       Michel       8450468             31
##  9 SEN       George       8445135             15
## 10 NYR       Jimmy        8445028             22

Separating and Uniting

Unite two columns

nhl_roster_unite <- small_nhl_rosters %>%

    unite(col = "Id + Jersey", c(player_id, sweater_number), sep = "=", )

Separate a column

nhl_roster_sep <- nhl_roster_unite %>%
    
    separate(col = "Id + Jersey", into = c("player_id", "sweater_number"))

Missing Values

NO MISSING VALUES