R Challenge #4: Water Research
The Software shall be used for Good,
not Evil,
Challenge: Import a JSON file into R
LinkedIn Learning just released a new course;?R Data Science Code Challenges. It's a collection of fifteen amusing problems to be solved with knowledge of the R language.
In the JSON challenge (part of the code challenge course), your job is to successfully import a JSON file with water data, then report?the level of the aquifer as well as the?date that that reading was made.?Here's the video description...
Here's My Solution
领英推荐
# retrieve the most current water level from the USGS API
# error trap the retrieval
# extract both the level and date from results
#install.packages("jsonlite")
library(jsonlite)
bullrunURL <- "https://waterservices.usgs.gov/nwis/iv/?format=json&sites=14138850¶meterCd=00060,00065&siteStatus=all"
bullrunWater <- tryCatch(fromJSON(bullrunURL),
? ? ? ? ? ? ? ? ? ? ? error = function(e) {message(paste("Error:",e))}
)
brw_waterLevel <- as.numeric(bullrunWater$value$timeSeries$values[[1]]$value[[1]]$value)
brw_time <- as.Date(bullrunWater$value$timeSeries$values[[1]]$value[[1]]$dateTime )
You'll notice I've used tryCatch to mitigate any problems with data retrieval. You might want to check out the extensive discussion on writing fault-resistant code published by the USGS - it's an excellent read.
Importing JSON isn't difficult...the tricky part is teasing the data out from the resulting list and the use of double-brackets instead of single brackets. Here's a two-minute video explaining [[]]...
The Hidden Story
I like using data from the U.S. Government (dot.gov) but they can be problematic. I used government weather data that had a bad habit of becoming unavailable. When the U.S. Congress doesn't approve the budget, government agencies (and their websites) shut down. I've also run into a situation where the organization sponsoring the data decides it shouldn't be available outside of the United States. Anyone with a URL that isn't dotCOM or dotNET is inexplicably blocked. This data is owned by the United States population - but agencies sometimes have opinions about this ownership. If you aren't already aware of internet freedom, take a minute to look at the Electronic Frontier Foundation.
How about you??
In this and future articles, I'll share some backstory behind each R challenge in this course. My peers have supplied other code challenges you might find interesting:?Javascript?...?Python?...?Java?...?Github?...?HTML?...?SQL?...?SQL for Data Science?...?PHP
Do you have some opinions on this challenge? Please share them in the comments below.
mnr