rnoaa - get GHCND data for all stations in a state

An rnoaa user asked essentially

How do I get all data for Oregon state for August 2016

I thought a solution to this would be helpful to others.

It is now easier to do this due to a change I just pushed up to Github. So reinstall from Github first:

devtools::install_github("ropensci/rnoaa")
library("rnoaa")
library("dplyr")

Breaking down the steps:

  1. Get GHCND station data
  2. Filter to just stations in Oregon OR
  3. Get just station IDs
  4. Get a subset for brevity
  5. Pass each station ID to the ghcnd() function to get GHCND data for each station
  6. Combine all results into a single tibble

After you have a the single tibble its easy to go from there with your analysis/modeling/plotting/etc.

sta <- ghcnd_stations()
sta %>%
  filter(state == "OR") %>% 
  .$id %>% 
  .[1:60] %>% 
  Map(ghcnd, .) %>%
  bind_rows %>% 
  filter(year == 2016, month == 8)
# A tibble: 32 × 128
            id  year month element VALUE1 MFLAG1 QFLAG1 SFLAG1 VALUE2 MFLAG2 QFLAG2 SFLAG2 VALUE3 MFLAG3
         <chr> <int> <int>   <chr>  <int>  <lgl>  <lgl>  <chr>  <int>  <lgl>  <lgl>  <chr>  <int>  <lgl>
1  US1ORBN0002  2016     8    PRCP     NA     NA     NA            NA     NA     NA            NA     NA
2  US1ORBN0002  2016     8    SNOW     NA     NA     NA            NA     NA     NA            NA     NA
3  US1ORBN0002  2016     8    PRCP     NA     NA     NA            NA     NA     NA            NA     NA
4  US1ORBN0002  2016     8    SNOW     NA     NA     NA            NA     NA     NA            NA     NA
5  US1ORBN0002  2016     8    PRCP     NA     NA     NA            NA     NA     NA            NA     NA
6  US1ORBN0002  2016     8    SNOW     NA     NA     NA            NA     NA     NA            NA     NA
7  US1ORBN0002  2016     8    PRCP     NA     NA     NA            NA     NA     NA            NA     NA
8  US1ORBN0002  2016     8    SNOW     NA     NA     NA            NA     NA     NA            NA     NA
9  US1ORBN0002  2016     8    PRCP     NA     NA     NA            NA     NA     NA            NA     NA
10 US1ORBN0002  2016     8    SNOW     NA     NA     NA            NA     NA     NA            NA     NA
# ... with 22 more rows, and 114 more variables: QFLAG3 <lgl>, SFLAG3 <chr>, VALUE4 <int>, MFLAG4 <lgl>,
#   QFLAG4 <chr>, SFLAG4 <chr>, VALUE5 <int>, MFLAG5 <lgl>, QFLAG5 <lgl>, SFLAG5 <chr>, VALUE6 <int>,
#   MFLAG6 <lgl>, QFLAG6 <lgl>, SFLAG6 <chr>, VALUE7 <int>, MFLAG7 <lgl>, QFLAG7 <lgl>, SFLAG7 <chr>,
#   VALUE8 <int>, MFLAG8 <lgl>, QFLAG8 <chr>, SFLAG8 <chr>, VALUE9 <int>, MFLAG9 <lgl>, QFLAG9 <lgl>,
#   SFLAG9 <chr>, VALUE10 <int>, MFLAG10 <lgl>, QFLAG10 <lgl>, SFLAG10 <chr>, VALUE11 <int>,
#   MFLAG11 <lgl>, QFLAG11 <lgl>, SFLAG11 <chr>, VALUE12 <int>, MFLAG12 <lgl>, QFLAG12 <lgl>, ...

Thank you for this. Very useful! Can the filter handle more than one variable? Say I wanted to filter on ‘state’ and ‘element’?

Do you mean this bit filter(year == 2016, month == 8) ?

If so, yes, it is doing 2 criteria there and you can do more as well. They have to evaluate to logical though.