Challenge 10

challenge_10
purrr
Author

Calvin Cuff

Published

January 28, 2024

The challenge

Use purrr with a function to perform some data science task. What this task is is up to you. It could involve computing summary statistics, reading in multiple datasets, running a random process multiple times, or anything else you might need to do in your work as a data analyst. You might consider using purrr with a function you wrote for challenge 9.

read_data function

We will redefine our read_data function from Challenge9 which will read in a given dataset of a generic file extension and return the data frame. It supports extension types of csv, xlsx, and xls. It also accepts a second argument denoting how many rows to skip when reading the data.

read_data <- function(file_name, num_skip) {
  file_extension <- tools::file_ext(file_name)
  
  data <- switch(file_extension,
    csv = read_csv(file_name, skip=num_skip),
    xlsx = read_excel(file_name, skip=num_skip),
    xls = read_excel(file_name, skip=num_skip),
    "Unsupported file type"
  )
  
  return(data)
}

purr

Using purrr we will call map2 with the read_data function to read a list of files as well as include the additional vectorized argument on how many lines to skip. This should allows us to iterate over the list of files, while respecting the individual skip counts. map2 will return a map of data frames which we can then pull out by position and assign each data frame to its own variable.

# List of files to read in
files <- list("data/railroad_2012_clean_county.csv","data/StateCounty2012.xls", "data/debt_in_trillions.xlsx", "unknown.txt")

# Number of lines to skip per file
skips <- list(0,3,0, 0)

# Return map of results from func
dfs <- map2(files, skips, read_data)

# Get data frames
railroad <- dfs[1]
state_county <- dfs[2]
debt <- dfs[3]
unknown <- dfs[4]

# Inspect
head(railroad)
[[1]]
# A tibble: 2,930 × 3
   state county               total_employees
   <chr> <chr>                          <dbl>
 1 AE    APO                                2
 2 AK    ANCHORAGE                          7
 3 AK    FAIRBANKS NORTH STAR               2
 4 AK    JUNEAU                             3
 5 AK    MATANUSKA-SUSITNA                  2
 6 AK    SITKA                              1
 7 AK    SKAGWAY MUNICIPALITY              88
 8 AL    AUTAUGA                          102
 9 AL    BALDWIN                          143
10 AL    BARBOUR                            1
# ℹ 2,920 more rows
head(state_county)
[[1]]
# A tibble: 2,990 × 5
   STATE     ...2  COUNTY               ...4  TOTAL
   <chr>     <lgl> <chr>                <lgl> <dbl>
 1 AE        NA    APO                  NA        2
 2 AE Total1 NA    <NA>                 NA        2
 3 AK        NA    ANCHORAGE            NA        7
 4 AK        NA    FAIRBANKS NORTH STAR NA        2
 5 AK        NA    JUNEAU               NA        3
 6 AK        NA    MATANUSKA-SUSITNA    NA        2
 7 AK        NA    SITKA                NA        1
 8 AK        NA    SKAGWAY MUNICIPALITY NA       88
 9 AK Total  NA    <NA>                 NA      103
10 AL        NA    AUTAUGA              NA      102
# ℹ 2,980 more rows
head(debt)
[[1]]
# A tibble: 74 × 8
   `Year and Quarter` Mortgage `HE Revolving` `Auto Loan` `Credit Card`
   <chr>                 <dbl>          <dbl>       <dbl>         <dbl>
 1 03:Q1                  4.94          0.242       0.641         0.688
 2 03:Q2                  5.08          0.26        0.622         0.693
 3 03:Q3                  5.18          0.269       0.684         0.693
 4 03:Q4                  5.66          0.302       0.704         0.698
 5 04:Q1                  5.84          0.328       0.72          0.695
 6 04:Q2                  5.97          0.367       0.743         0.697
 7 04:Q3                  6.21          0.426       0.751         0.706
 8 04:Q4                  6.36          0.468       0.728         0.717
 9 05:Q1                  6.51          0.502       0.725         0.71 
10 05:Q2                  6.70          0.528       0.774         0.717
# ℹ 64 more rows
# ℹ 3 more variables: `Student Loan` <dbl>, Other <dbl>, Total <dbl>
head(unknown)
[[1]]
[1] "Unsupported file type"