# 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
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