Extract variables

  • separate_wider_regex(): extract data out of one column into one or more new columns.
    • construct a sequence of regular expressions that match each piece.
    • give each a name to have the contents of that piece to appear in the output.
df <- tribble(
  ~str,
  "<Sheryl>-F_34",
  "<Kisha>-F_45", 
  "<Brandon>-N_33",
  "<Sharon>-F_38", 
  "<Penny>-F_58",
  "<Justin>-M_41", 
  "<Patricia>-F_84", 
)


df |> 
  separate_wider_regex(
    str,
    patterns = c(
      "<", 
      name = "[A-Za-z]+", 
      ">-", 
      gender = ".",
      "_",
      age = "[0-9]+"
    )
  )
## # A tibble: 7 × 3
##   name     gender age  
##   <chr>    <chr>  <chr>
## 1 Sheryl   F      34   
## 2 Kisha    F      45   
## 3 Brandon  N      33   
## 4 Sharon   F      38   
## 5 Penny    F      58   
## 6 Justin   M      41   
## 7 Patricia F      84