Skip to content

Instantly share code, notes, and snippets.

@mtdukes
Created July 28, 2021 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtdukes/54d198f4b336216824205599d794f885 to your computer and use it in GitHub Desktop.
Save mtdukes/54d198f4b336216824205599d794f885 to your computer and use it in GitHub Desktop.
Simple script to import data on COVID-19 from the CDC website.
'''
A simple script to grab COVID data from the CDC
hastily typed by @mtdukes
Follow the Twitter thread for more detail:
https://twitter.com/mtdukes/status/1420191934979657731
'''
#install your packages, if you haven't already
install.packages('jsonlite')
install.packages('tidyverse')
#load libraries into your workspace
library('jsonlite')
library('tidyverse')
#load json data from the CDC URL
county_data <- fromJSON(
'https://covid.cdc.gov/covid-data-tracker/COVIDData/getAjaxData?id=integrated_county_latest_external_data',
simplifyVector = TRUE
#the json file includes another item that we don't want, so specify what we're interested in
)['integrated_county_latest_external_data'] %>%
#turn it into a dataframe instead of a weird matrix
as.data.frame() %>%
#clean up the column names
rename_with( ~ tolower( gsub('integrated_county_latest_external_data\\.' ,'', . ) ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment