You've successfully subscribed to MyPad Blog
Great! Next, complete checkout for full access to MyPad Blog
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Recursively download data - Tidyverse way

Recursively download data - Tidyverse way

Thinking of trying my hand at participating in this week's #TidyTuesday exercise.

I'll take this in small steps and hopefully get to the end.

Here is an approach to get the data into Rstudio!


# credit:
# [Tom Mock on Twitter: "The @R4DScommunity welcomes you to week 9 of #TidyTuesday! We're exploring Measles vaccination!! 📁 📰 #r4ds #tidyverse #rstats #dataviz" / Twitter](

# Data for each individual school
url_all <- ""
dt_all <- fread(url_all)

# More generalized data by state counties or state school districts
url_state_overview <- ""
dt_state_overview <- fread(url_state_overview)

# Same data as all-measles-rates, but seperated by state
# example here
url_state_arizona <- ""
dt_state_arizona <- fread(url_state_arizona)

# need to learn to use tidyverse

df_all <- as_tibble(dt_all)

# get state data as filters on df_all
df_all %>% 
select(state) %>% 
distinct() %>% 
 .$state %>% 
walk(., ~ assign(paste0("df_",tolower(.x)), df_all %>% filter(state==.x), envir=.GlobalEnv))

# lets actually download the state data! ... straight from github
# WSJ provided it in a folder organized by state name
get_state_measles_data <- function(state) {
      print(glue("downloading data for ... {state}"))

      # supressing message ... silent ..
      d <- read_csv(glue("{state}.csv"), col_types = cols())
      assign(glue("df_{state}"), d, envir=.GlobalEnv) 
    error = function(error) {
      print(glue("error while downloading data for ... {state}"))

# get the states
df_all %>% 
select(state) %>% 
distinct() %>% 
mutate(state = tolower(state)) %>% 
.$state -> states

glue("# of states: {NROW(states)}")

walk(states, get_state_measles_data)

# working with lists in tidy: [jennybc/repurrrsive: Recursive lists to use in teaching and examples, because there is no iris data for lists.](