R Challenge #4: Water Research

R Challenge #4: Water Research

The Software shall be used for Good,
not Evil,

...from the JSON license

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&parameterCd=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

要查看或添加评论,请登录

Mark Niemann-Ross的更多文章

  • Documenting My Code ... For Me

    Documenting My Code ... For Me

    There are two signs of old age: old age, and ..

  • R Meets Hardware

    R Meets Hardware

    R is a programming language for statistical computing and data visualization. It has been adopted in the fields of data…

    2 条评论
  • Party Buzz Kill: modifying data

    Party Buzz Kill: modifying data

    So Steve (SQL), Marsha (C), Bob (Python), and I (R) are at this party. We have TOTALLY cleared the room, especially now…

    2 条评论
  • Rain - Evapotranspiration = mm Water

    Rain - Evapotranspiration = mm Water

    "Eeee-VAP-oooo-TRANS-PURR-ation," I savor the word as I release it into our conversation. I'm still at the party with…

  • Party Buzz Kill: Data Storage

    Party Buzz Kill: Data Storage

    I'm at this party where Bob and Marsha and I are discussing the best languages for programming a Raspberry Pi. Bob…

    5 条评论
  • R Waters My Garden

    R Waters My Garden

    I'm at a party, and the topic of programming languages comes up. A quarter of the room politely leaves, another half…

    10 条评论
  • Caning and Naming

    Caning and Naming

    We've been back from Port Townsend for a week. Progress on the boat isn't as dramatic as it is when we're spending the…

    1 条评论
  • Irrigate with R and Raspberry Pi

    Irrigate with R and Raspberry Pi

    I’m working on my irrigation system. This requires a controller to turn it on and off.

    3 条评论
  • 5 Reasons to Learn Natural Language Processing with R

    5 Reasons to Learn Natural Language Processing with R

    Why learn R? Why learn Natural Language Processing? Here's five reasons..

    1 条评论
  • Performing Natural Language Processing with R

    Performing Natural Language Processing with R

    I recently released a course on Educative covering topics in Natural Language Processing. Different Learners -…

    1 条评论

社区洞察

其他会员也浏览了