quickerstats

NOT_CRAN <- identical(tolower(Sys.getenv("NOT_CRAN")), "true")
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  purl = NOT_CRAN
)

Introduction

The USDA's National Agricultural Statistics Service collects a wide variety of data on US agriculture. They maintain a web tool (Quick Stats), and an API for accessing the data. While powerful and flexible, both of the existing tools for pulling data can be cumbersome for many users. The goal of quickerstats is to functionality for pulling data that is easier to use. The package does this in two ways:

See:
https://quickstats.nass.usda.gov/
https://quickstats.nass.usda.gov/api

Installation and setup

``` {r, eval = FALSE} devtools::install_github('anderaa/quickerstats', force=TRUE, build_vignettes=TRUE)

```r
library('quickerstats')

You will need an API key from NASS. Go to https://quickstats.nass.usda.gov/api. I recommend storing your key as an environmental variable. To do so, in your console type:

file.edit("~/.Renviron")

And then add a line to this file:

NASS_KEY='your_nass_key'

Save the file, and restart your R session. You can now access the variable with:

key <- Sys.getenv('NASS_KEY')

Search for a data item

Search for a data item like this:

items <- search_data_items(key=key, search_terms=c('corn', 'harvested'), exclude=c('sweet'))
print(items[1:5])

This will give you a list of data items. Each will contain all of the search terms and none of the exclude terms. The exclude argument is optional. A typical workflow would be to start broad, view results, then incrementally narrow your search using both arguments. Note that the search terms are not case sensitive - the function handles this automatically.

You can also retrieve options associated with a data item: ``` {r, message = FALSE, eval = NOT_CRAN} df <- get_options(key=key, data_item='CORN, GRAIN - ACRES HARVESTED') print(head(df, 5))

## Retrieve data
Once you have found your desired data item, you can pull data in one of five 
ways:

#### 1. Pull state-level data for all states
To pull data for all states, the fips argument must be set to 'all':
``` {r, message = FALSE, eval = NOT_CRAN}
df <- get_state_data(key=key, year=2017, data_item='CORN, GRAIN - ACRES HARVESTED', fips='all')
print(head(df, 5))

2. Pull state-level data for a single state

To pull data for a single county, the fips argument must be passed a 2-character string that is the state FIPS code: ``` {r, message = FALSE, eval = NOT_CRAN} df <- get_state_data(key=key, year=2017, data_item='CORN, GRAIN - ACRES HARVESTED', fips='08') print(head(df, 5))

#### 3. Pull county-level data for all counties in the US
As in the state example, set fips to 'all':
``` {r, message = FALSE, eval = NOT_CRAN}
df <- get_county_data(key=key, year=2017, data_item='CORN, GRAIN - ACRES HARVESTED', fips='all')
print(head(df, 5))

4. Pull county-level data for all counties in a single state

To get data for counties in a state, fips must be the 2-character fips code of the desired state: ``` {r, message = FALSE, eval = NOT_CRAN} df <- get_county_data(key=key, year=2017, data_item='CORN, GRAIN - ACRES HARVESTED', fips='08') print(head(df, 5))

#### 5. Pull county-level data for a single county
Simple pass fips a 5-character FIPS code for the county:
``` {r, message = FALSE, eval = NOT_CRAN}
df <- get_county_data(key=key, year=2017, data_item='CORN, GRAIN - ACRES HARVESTED', fips='08069')
print(head(df, 5))

Function documentation

{r, eval = FALSE} ?search_data_items ?get_options ?get_county_item_count ?get_county_data ?get_state_item_count ?get_state_data



Try the quickerstats package in your browser

Any scripts or data that you put into this service are public.

quickerstats documentation built on Oct. 23, 2020, 8:11 p.m.