Import your data

season_goals <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-03/season_goals.csv')
## Rows: 4810 Columns: 23
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (9): position, hand, player, years, status, season, team, league, headshot
## dbl (14): rank, total_goals, yr_start, age, season_games, goals, assists, po...
## 
## ℹ 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.
data_small <- season_goals %>% 
    slice(1:5) %>% 
    select(player, total_goals:team)

data_small
## # A tibble: 5 × 7
##   player        total_goals status  yr_start season    age team 
##   <chr>               <dbl> <chr>      <dbl> <chr>   <dbl> <chr>
## 1 Wayne Gretzky         894 Retired     1979 1978-79    18 TOT  
## 2 Wayne Gretzky         894 Retired     1979 1978-79    18 INR  
## 3 Wayne Gretzky         894 Retired     1979 1978-79    18 EDO  
## 4 Wayne Gretzky         894 Retired     1979 1979-80    19 EDM  
## 5 Wayne Gretzky         894 Retired     1979 1980-81    20 EDM

Pivoting

long to wide form

data_wide <- data_small %>% 
    pivot_wider(names_from = season, values_from = total_goals)

data_wide
## # A tibble: 5 × 8
##   player        status  yr_start   age team  `1978-79` `1979-80` `1980-81`
##   <chr>         <chr>      <dbl> <dbl> <chr>     <dbl>     <dbl>     <dbl>
## 1 Wayne Gretzky Retired     1979    18 TOT         894        NA        NA
## 2 Wayne Gretzky Retired     1979    18 INR         894        NA        NA
## 3 Wayne Gretzky Retired     1979    18 EDO         894        NA        NA
## 4 Wayne Gretzky Retired     1979    19 EDM          NA       894        NA
## 5 Wayne Gretzky Retired     1979    20 EDM          NA        NA       894

wide to long form

data_wide %>% 
    pivot_longer(cols           = `1978-79`:`1980-81`, 
                 names_to       = "season", 
                 values_to      = "total_goals", 
                 values_drop_na = TRUE)
## # A tibble: 5 × 7
##   player        status  yr_start   age team  season  total_goals
##   <chr>         <chr>      <dbl> <dbl> <chr> <chr>         <dbl>
## 1 Wayne Gretzky Retired     1979    18 TOT   1978-79         894
## 2 Wayne Gretzky Retired     1979    18 INR   1978-79         894
## 3 Wayne Gretzky Retired     1979    18 EDO   1978-79         894
## 4 Wayne Gretzky Retired     1979    19 EDM   1979-80         894
## 5 Wayne Gretzky Retired     1979    20 EDM   1980-81         894

Separating and Uniting

Unite two columns

data_united <- data_small %>%
    
    unite(col = "newName", team, season, sep = "/", remove = TRUE)

Separate a column

data_united %>%
    
    separate(col = newName, into = c("team", "season"), sep = "/")
## # A tibble: 5 × 7
##   player        total_goals status  yr_start team  season    age
##   <chr>               <dbl> <chr>      <dbl> <chr> <chr>   <dbl>
## 1 Wayne Gretzky         894 Retired     1979 TOT   1978-79    18
## 2 Wayne Gretzky         894 Retired     1979 INR   1978-79    18
## 3 Wayne Gretzky         894 Retired     1979 EDO   1978-79    18
## 4 Wayne Gretzky         894 Retired     1979 EDM   1979-80    19
## 5 Wayne Gretzky         894 Retired     1979 EDM   1980-81    20

Missing Values