Check out this purrr tutorial by Jenny Bryan: https://jennybc.github.io/purrr-tutorial/ls02_map-extraction-advanced.html

Additional content to read up on: https://jennybc.github.io/purrr-tutorial/ls01_map-name-position-shortcuts.html

  1. Load the UFO dataset:
ufo <- read_csv(
"https://sisbid.github.io/Data-Wrangling/data/ufo/ufo_data_complete.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details, e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 88875 Columns: 11
## ── Column specification ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): datetime, city, state, country, shape, duration (hours/min), comme...
## dbl  (1): duration (seconds)
## 
## ℹ 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.
  1. Working with the ufo data, use mutate and across to change the character variables (where(is.character)) to be uppercase with toupper.
ufo |>
  mutate(across(
    where(is.character),
    toupper
  ))
## # A tibble: 88,875 × 11
##    datetime         city                state country shape `duration (seconds)`
##    <chr>            <chr>               <chr> <chr>   <chr>                <dbl>
##  1 10/10/1949 20:30 SAN MARCOS          TX    US      CYLI…                 2700
##  2 10/10/1949 21:00 LACKLAND AFB        TX    <NA>    LIGHT                 7200
##  3 10/10/1955 17:00 CHESTER (UK/ENGLAN… <NA>  GB      CIRC…                   20
##  4 10/10/1956 21:00 EDNA                TX    US      CIRC…                   20
##  5 10/10/1960 20:00 KANEOHE             HI    US      LIGHT                  900
##  6 10/10/1961 19:00 BRISTOL             TN    US      SPHE…                  300
##  7 10/10/1965 21:00 PENARTH (UK/WALES)  <NA>  GB      CIRC…                  180
##  8 10/10/1965 23:45 NORWALK             CT    US      DISK                  1200
##  9 10/10/1966 20:00 PELL CITY           AL    US      DISK                   180
## 10 10/10/1966 21:00 LIVE OAK            FL    US      DISK                   120
## # ℹ 88,865 more rows
## # ℹ 5 more variables: `duration (hours/min)` <chr>, comments <chr>,
## #   `date posted` <chr>, latitude <chr>, longitude <chr>
  1. Now do the same thing as question one, but this time use modify_if.
ufo |>
  modify_if(is.character, toupper)
## # A tibble: 88,875 × 11
##    datetime         city                state country shape `duration (seconds)`
##    <chr>            <chr>               <chr> <chr>   <chr>                <dbl>
##  1 10/10/1949 20:30 SAN MARCOS          TX    US      CYLI…                 2700
##  2 10/10/1949 21:00 LACKLAND AFB        TX    <NA>    LIGHT                 7200
##  3 10/10/1955 17:00 CHESTER (UK/ENGLAN… <NA>  GB      CIRC…                   20
##  4 10/10/1956 21:00 EDNA                TX    US      CIRC…                   20
##  5 10/10/1960 20:00 KANEOHE             HI    US      LIGHT                  900
##  6 10/10/1961 19:00 BRISTOL             TN    US      SPHE…                  300
##  7 10/10/1965 21:00 PENARTH (UK/WALES)  <NA>  GB      CIRC…                  180
##  8 10/10/1965 23:45 NORWALK             CT    US      DISK                  1200
##  9 10/10/1966 20:00 PELL CITY           AL    US      DISK                   180
## 10 10/10/1966 21:00 LIVE OAK            FL    US      DISK                   120
## # ℹ 88,865 more rows
## # ℹ 5 more variables: `duration (hours/min)` <chr>, comments <chr>,
## #   `date posted` <chr>, latitude <chr>, longitude <chr>
  1. Which of question 1 or 2 is faster? Try piping each response into system.time().
ufo |>
  mutate(across(
    where(is.character),
    toupper
  )) |> system.time()
##    user  system elapsed 
##   0.297   0.001   0.298
ufo |>
  modify_if(is.character, toupper) |>
  system.time()
##    user  system elapsed 
##   0.294   0.001   0.294
  1. Use group_split() to split up the ufo dataset by country. Assign the list to the object ufo_list

EG: ??? <- ??? |> group_by(???) |> group_split()

ufo_list <- ufo |> group_by(country) |> group_split()
  1. Add the list keys.

EG: ufo_keys <- ??? |> group_by(???) |> group_keys() |> pull(???) names(ufo_list) <- ufo_keys

ufo_keys <- ufo |> group_by(country) |> group_keys() |> pull(country)
names(ufo_list) <- ufo_keys
  1. For each of the datasets in ufo_list, map the anonymous function: \(x) count(x, shape)
ufo_list |>
  map(\(x) count(x, shape))
## $au
## # A tibble: 22 × 2
##    shape        n
##    <chr>    <int>
##  1 changing    10
##  2 chevron      3
##  3 cigar       18
##  4 circle      68
##  5 cone         8
##  6 cross        3
##  7 cylinder    10
##  8 diamond     11
##  9 disk        62
## 10 egg         12
## # ℹ 12 more rows
## 
## $ca
## # A tibble: 23 × 2
##    shape        n
##    <chr>    <int>
##  1 changing    73
##  2 chevron     38
##  3 cigar       76
##  4 circle     308
##  5 cone        13
##  6 cross        9
##  7 cylinder    56
##  8 delta        1
##  9 diamond     49
## 10 disk       217
## # ℹ 13 more rows
## 
## $de
## # A tibble: 19 × 2
##    shape         n
##    <chr>     <int>
##  1 changing      2
##  2 chevron       1
##  3 cigar         3
##  4 circle       10
##  5 cylinder      3
##  6 diamond       3
##  7 disk          6
##  8 egg           2
##  9 fireball     10
## 10 flash         1
## 11 formation     3
## 12 light        24
## 13 other         9
## 14 oval          8
## 15 rectangle     1
## 16 sphere        7
## 17 triangle      9
## 18 unknown       8
## 19 <NA>          2
## 
## $gb
## # A tibble: 22 × 2
##    shape        n
##    <chr>    <int>
##  1 changing    47
##  2 chevron     10
##  3 cigar       68
##  4 circle     259
##  5 cone        13
##  6 cross       10
##  7 cylinder    31
##  8 diamond     48
##  9 disk       111
## 10 egg         35
## # ℹ 12 more rows
## 
## $us
## # A tibble: 29 × 2
##    shape        n
##    <chr>    <int>
##  1 changed      1
##  2 changing  1708
##  3 chevron    843
##  4 cigar     1750
##  5 circle    6650
##  6 cone       278
##  7 crescent     1
##  8 cross      214
##  9 cylinder  1089
## 10 delta        7
## # ℹ 19 more rows
## 
## [[6]]
## # A tibble: 24 × 2
##    shape        n
##    <chr>    <int>
##  1 changing   300
##  2 chevron    112
##  3 cigar      326
##  4 circle    1158
##  5 cone        55
##  6 crescent     1
##  7 cross       29
##  8 cylinder   193
##  9 diamond    191
## 10 disk      1054
## # ℹ 14 more rows