[API] rnassqs

API & Databases R Courses

Access USDA agricultural data through the rnassqs API.

Thierry Warin https://warin.ca/aboutme.html (HEC Montréal and CIRANO (Canada))https://www.hec.ca/en/profs/thierry.warin.html
01-29-2020

Database description

“The United States Deparment of Agriculture National Agricultural Statistics Service (USDA-NASS) provides a wide range of agricultural data that includes animal, crop, demographic, economic, and environmental measures across a number of geographies and time periods.” (accessing USDA agricultural data via API, Nicholas Potter)

United States Department of Agriculture : https://www.nass.usda.gov/

API key

To access the API’s functions, a key is necessary. To do so, request a key at this link: https://quickstats.nass.usda.gov/api

Functions

This API lets you download data from the Quick Stats database. The database is maintained by the United States Department of Agriculture. The API allows you to search and analyse data based on certain parameters.

Each of these functions are detailed in this course and some examples are provided.

nassqs_params()

The nassqs_params() return a list of NASS QS parameters. This function could be usefull to filter the desired research.

For example, we would like to obtain a list of all the available parameters for a specefic research.

 library(rnassqs)

# Register API key
NASSQS_TOKEN="05F23236-C710-3F1E-B721-E2E00ABD88FA"

# You must set your api key before requesting data
nassqs_auth(key = "05F23236-C710-3F1E-B721-E2E00ABD88FA")

# Available parameters
nassqs_params()
 [1] "agg_level_desc"        "asd_code"             
 [3] "asd_desc"              "begin_code"           
 [5] "class_desc"            "commodity_desc"       
 [7] "congr_district_code"   "country_code"         
 [9] "country_name"          "county_ansi"          
[11] "county_code"           "county_name"          
[13] "CV"                    "domaincat_desc"       
[15] "domain_desc"           "end_code"             
[17] "freq_desc"             "group_desc"           
[19] "load_time"             "location_desc"        
[21] "prodn_practice_desc"   "reference_period_desc"
[23] "region_desc"           "sector_desc"          
[25] "short_desc"            "state_alpha"          
[27] "state_ansi"            "state_name"           
[29] "state_fips_code"       "statisticcat_desc"    
[31] "source_desc"           "unit_desc"            
[33] "util_practice_desc"    "Value"                
[35] "watershed_code"        "watershed_desc"       
[37] "week_ending"           "year"                 
[39] "zip_5"                

nassqs()

Once you have selected the prefered parameters for your research, the function nassqs() takes as an input these parameters. It returns a data frame of the selected parameters.

For example, it would be interesting to download the data of the corn yields from 2017 onward for counties in Virginia and Pennsylvania.

# Download the data from the Quick Stats database.
df <- nassqs(commodity_desc = "CORN",
             year__GE = 2017,
             agg_level_desc = "COUNTY",
             state_alpha = c("VA", "PA"),
             statisticcat_desc = "YIELD")

  |                                                                  
  |                                                            |   0%
  |                                                                  
  |=                                                           |   1%
  |                                                                  
  |=                                                           |   2%
  |                                                                  
  |===                                                         |   5%
  |                                                                  
  |=====                                                       |   8%
  |                                                                  
  |=======                                                     |  12%
  |                                                                  
  |=========                                                   |  16%
  |                                                                  
  |============                                                |  19%
  |                                                                  
  |============                                                |  20%
  |                                                                  
  |==============                                              |  24%
  |                                                                  
  |===============                                             |  24%
  |                                                                  
  |===============                                             |  25%
  |                                                                  
  |=================                                           |  29%
  |                                                                  
  |==================                                          |  29%
  |                                                                  
  |====================                                        |  33%
  |                                                                  
  |====================                                        |  34%
  |                                                                  
  |=======================                                     |  38%
  |                                                                  
  |=======================                                     |  39%
  |                                                                  
  |==========================                                  |  43%
  |                                                                  
  |============================                                |  47%
  |                                                                  
  |=============================                               |  48%
  |                                                                  
  |===============================                             |  52%
  |                                                                  
  |================================                            |  53%
  |                                                                  
  |==================================                          |  56%
  |                                                                  
  |====================================                        |  60%
  |                                                                  
  |====================================                        |  61%
  |                                                                  
  |=====================================                       |  61%
  |                                                                  
  |=======================================                     |  65%
  |                                                                  
  |=======================================                     |  66%
  |                                                                  
  |==========================================                  |  70%
  |                                                                  
  |=============================================               |  74%
  |                                                                  
  |=============================================               |  75%
  |                                                                  
  |===============================================             |  79%
  |                                                                  
  |==================================================          |  83%
  |                                                                  
  |====================================================        |  87%
  |                                                                  
  |======================================================      |  91%
  |                                                                  
  |=======================================================     |  91%
  |                                                                  
  |=========================================================   |  95%
  |                                                                  
  |=========================================================   |  96%
  |                                                                  
  |==========================================================  |  96%
  |                                                                  
  |============================================================| 100%

tl;dr

library(rnassqs)
# Register API key
NASSQS_TOKEN="<your api key here>"
# You must set your api key before requesting data
nassqs_auth(key = <your api key>)

# Available parameters
nassqs_params()

# Download the data from the Quick Stats database.
df <- nassqs(commodity_desc = "CORN",
             year__GE = 2017,
             agg_level_desc = "COUNTY",
             state_alpha = c("VA", "PA"),
             statisticcat_desc = "YIELD")

Code learned this week

Command Detail
nassqs_params() list of NASS QS parameters
nassqs() Find data related to chosen pararameters

References

This tutorial uses the R documentation


Citation

For attribution, please cite this work as

Warin (2020, Jan. 29). Thierry Warin, PhD: [API] rnassqs. Retrieved from https://warin.ca/posts/api-rnassqs/

BibTeX citation

@misc{warin2020[api],
  author = {Warin, Thierry},
  title = {Thierry Warin, PhD: [API] rnassqs},
  url = {https://warin.ca/posts/api-rnassqs/},
  year = {2020}
}