R: Packages and Data Retrieval Hydroinformatics – Fall 2015
Learning Objectives Describe the difference between packages and libraries in R Install and load packages Use documentation and other resources to learn how to use unfamiliar packages Use data retrieval packages and web services to obtain hydrologic data
R Packages and Libraries Package: R functions, data, and compiled code in a well- defined format. Library: The directory where packages are stored. Implement many common data analysis and statistical procedures Provide excellent graphics functionality Serve as a starting point for many data analysis tasks A huge community of R developers exist – it’s likely that there’s an R package for many of the tasks you commonly do
USGS Data Retrieval Package for R Collection of functions to help retrieve hydrologic and water quality data using web services: U.S. Geological Survey (USGS) using National Water Information System (NWIS) tools U.S. Environmental Protection Agency (EPA) Data access is through web services https://github.com/USGS-R/dataRetrieval
Installing and loading Packages library() #displays available packages Tools>Install Packages or install.packages(“package_name”) library(package_name) #loads package
Demo – dataRetrieval We want to automate the retrieval and import of data from a specific site into R using the dataRetrieval package. But how? https://github.com/USGS-R/dataRetrieval Use the source, Luke!
Demo – dataRetrieval for Gage Height Try: Retrieve USGS gage height data for any site in Utah for May 2014. What function should you use? This package is well documented and its examples are very useful for modifying/tailoring to specific needs. For example, this code uses “service” (look at the examples and see that it is “iv” for one readNWISdata() and it is “site” for another. What do these terms mean, and where can you find out about them?
Demo – dataRetrieval for Gage Height How do you find the parameter code for gage height? Explore the github documentation: If you know a package can help you, but you don’t know how to use the functions, you may have to explore! Find examples that you can modify: Search the comments in the function code:
Demo – dataRetrieval for Gage Height Find site code: #Then modify the other example code with your site ID and parameter code instGage <- readNWISdata(sites="10171000", service="iv",parameterCd="00065", startDate='2014-05-01T00:00Z',endDate='2014- 05-31T00:00Z') UT 00065 From the examples in the readNWISdata function
WaterML Package for R WaterML is a standard information model for the representation of water observations data, with the intent of allowing the exchange of such data sets across information systems.
WaterML Package for R Developed as part of a semester project from last year, Jiri and Bryn from BYU
WaterML Package for R retrieve and analyze data from HydroServers of multiple organizations that are listed in the CUAHSI Water Data Center catalog great example of semester project
Demo - WaterML Try downloading two variables from Red Butte Creek, Logan River, or Provo River GAMUT sites and fit a regression model between the two datasets https://www.cuahsi.org/Files/Posts/documents/WDC_Hand out.pdf