# Load package
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
departures <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-04-27/departures.csv')
## Rows: 9423 Columns: 19
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (8): coname, exec_fullname, interim_coceo, still_there, notes, sources...
## dbl  (10): dismissal_dataset_id, gvkey, fyear, co_per_rol, departure_code, c...
## dttm  (1): leftofc
## 
## ℹ 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.
set.seed(1234)
depart_small <- sample_n(departures, 5) %>% select(coname, exec_fullname, cik)
depart_small
## # A tibble: 5 × 3
##   coname              exec_fullname               cik
##   <chr>               <chr>                     <dbl>
## 1 UTI WORLDWIDE INC   Eric W. Kirchner        1124827
## 2 ACCENTURE PLC       David P. Rowland        1467373
## 3 INTREPID POTASH INC Robert P. Jornayvaz III 1421461
## 4 MCDERMOTT INTL INC  David Dickson            708819
## 5 SOURCEFIRE INC      John C. Burris          1168195

Tidy data

Pivoting

long to wide form

depart_wide <- depart_small %>%
    
    pivot_wider(names_from = coname, 
                values_from = cik)

depart_wide
## # A tibble: 5 × 6
##   exec_fullname        `UTI WORLDWIDE INC` `ACCENTURE PLC` `INTREPID POTASH INC`
##   <chr>                              <dbl>           <dbl>                 <dbl>
## 1 Eric W. Kirchner                 1124827              NA                    NA
## 2 David P. Rowland                      NA         1467373                    NA
## 3 Robert P. Jornayvaz…                  NA              NA               1421461
## 4 David Dickson                         NA              NA                    NA
## 5 John C. Burris                        NA              NA                    NA
## # ℹ 2 more variables: `MCDERMOTT INTL INC` <dbl>, `SOURCEFIRE INC` <dbl>

wide to long form

depart_wide %>%
    
    pivot_longer(cols = `UTI WORLDWIDE INC`:`SOURCEFIRE INC`, 
                 values_drop_na = TRUE)
## # A tibble: 5 × 3
##   exec_fullname           name                  value
##   <chr>                   <chr>                 <dbl>
## 1 Eric W. Kirchner        UTI WORLDWIDE INC   1124827
## 2 David P. Rowland        ACCENTURE PLC       1467373
## 3 Robert P. Jornayvaz III INTREPID POTASH INC 1421461
## 4 David Dickson           MCDERMOTT INTL INC   708819
## 5 John C. Burris          SOURCEFIRE INC      1168195

Separating and Uniting

Separate a column

depart_small %>%
    
    separate(col = exec_fullname, into = c("first_name", "Middle_inital", "last_name"))
## Warning: Expected 3 pieces. Additional pieces discarded in 1 rows [3].
## Warning: Expected 3 pieces. Missing pieces filled with `NA` in 1 rows [4].
## # A tibble: 5 × 5
##   coname              first_name Middle_inital last_name     cik
##   <chr>               <chr>      <chr>         <chr>       <dbl>
## 1 UTI WORLDWIDE INC   Eric       W             Kirchner  1124827
## 2 ACCENTURE PLC       David      P             Rowland   1467373
## 3 INTREPID POTASH INC Robert     P             Jornayvaz 1421461
## 4 MCDERMOTT INTL INC  David      Dickson       <NA>       708819
## 5 SOURCEFIRE INC      John       C             Burris    1168195

Unite two columns

depart_small %>%
    
    unite(col = "rate", c(coname,exec_fullname), sep = "/", )
## # A tibble: 5 × 2
##   rate                                            cik
##   <chr>                                         <dbl>
## 1 UTI WORLDWIDE INC/Eric W. Kirchner          1124827
## 2 ACCENTURE PLC/David P. Rowland              1467373
## 3 INTREPID POTASH INC/Robert P. Jornayvaz III 1421461
## 4 MCDERMOTT INTL INC/David Dickson             708819
## 5 SOURCEFIRE INC/John C. Burris               1168195