Add double quote after pip %>% in tidyverse

Hi guys, I am new to tidyverse. I am trying to use stationaRy package to download NOAA data.
Here is the scripts.

library(stationaRy )
Station <-  get_station_metadata() %>%
  dplyr::filter(country == "CH" | name == "ANDA") %>%
  dplyr::pull(id) %>% get_met_data(years = 2020)

The output is the error as it.

.....doesn't have a record in the get_station_metadata() table.

It's works with the following.

Station <- get_met_data(id = "450010-99999", years = 2020)

It seems that the get_met_data functin is only worked for the id with double quote. How to add double quote to the output of pull function.

Best,

Hello @Pin-Jun ,

welcome in this community.

The problem with your code is not that the station_id has to be provided with double quotes.
That is just the way the user has to write a character value (single quotes are also okay).
The result of the dplyr::pull is character so it can be passed as first argument to the get_met_data.
The current problem is that your filter returns a data.frame of 1333 rows. The get_met_data function accepts one station_id and you give it 1333 of these.

There are some other problems with the functions you use:

  • if a station has no data for 2020 the functionget_met_data fails ungracefully. So you should check the result of the get_station_metadata if there is data for 2020.
    In the code below I split your code in a section that returns all data (in Stations) and a section that returns the station_ids, so that you can check this. The error message for a station without 2020 data:

z <- get_met_data(station_ids[1],years = 2020)
Error in seq.int(0, to0 - from, by) : 'to' must be a finite number
In addition: Warning messages:
1: All formats failed to parse. No formats found.
2: All formats failed to parse. No formats found.

  • From the Stations with 2020 data I randomly chose two station_ids (with row numbers 956 and 976) to show you how read more than one station_id with a call of purrr::map.
  • In Stations I see that the station with row number 976 should end 08Jan2020 but I see that there is data for the whole of 2020, so I wonder what the value is of the end_date
  • Also for the two stations with 2020 data I get warnings

Problem with mutate() column temp.
i temp = dplyr::case_when(temp == 9999 ~ NA_real_, TRUE ~ temp/10).
i One or more parsing issues, see problems() for details

In the example below I retrieve the two data.frames in a list. By using the purrr::map_dfr function they can be gathered in one data.frame but in that case a column should be added with the station_id.
I hope this helps.

library(stationaRy )
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(purrr)

Stations <-  get_station_metadata() %>%
  dplyr::filter(country == "CH" | name == "ANDA") 
station_ids <- Stations %>% dplyr::pull(id)
length(station_ids)
#> [1] 1333

y956_976 <- purrr::map(station_ids[c(956,976)],
                       ~get_met_data(station_id=.,years = 2020) )
#> Warning: One or more parsing issues, see `problems()` for details

#> Warning: One or more parsing issues, see `problems()` for details

#> Warning: One or more parsing issues, see `problems()` for details

#> Warning: One or more parsing issues, see `problems()` for details
str(y956_976)
#> List of 2
#>  $ : tibble [8,784 x 10] (S3: tbl_df/tbl/data.frame)
#>   ..$ id        : chr [1:8784] "579320-99999" "579320-99999" "579320-99999" "579320-99999" ...
#>   ..$ time      : POSIXct[1:8784], format: "2020-01-01 00:00:00" "2020-01-01 01:00:00" ...
#>   ..$ temp      : num [1:8784] NA NA NA NA NA NA NA NA NA NA ...
#>   ..$ wd        : num [1:8784] NA NA 15 NA NA 259 NA NA 61 NA ...
#>   ..$ ws        : num [1:8784] NA NA 0.7 NA NA 0.6 NA NA 1.9 NA ...
#>   ..$ atmos_pres: num [1:8784] NA NA 1028 NA NA ...
#>   ..$ dew_point : num [1:8784] NA NA NA NA NA NA NA NA NA NA ...
#>   ..$ rh        : num [1:8784] NA NA NA NA NA NA NA NA NA NA ...
#>   ..$ ceil_hgt  : num [1:8784] NA NA NA NA NA NA NA NA NA NA ...
#>   ..$ visibility: num [1:8784] NA NA 3800 NA NA 1900 NA NA 3300 NA ...
#>  $ : tibble [8,784 x 10] (S3: tbl_df/tbl/data.frame)
#>   ..$ id        : chr [1:8784] "580270-99999" "580270-99999" "580270-99999" "580270-99999" ...
#>   ..$ time      : POSIXct[1:8784], format: "2020-01-01 00:00:00" "2020-01-01 01:00:00" ...
#>   ..$ temp      : num [1:8784] NA NA -0.6 NA NA -0.5 NA NA -0.2 NA ...
#>   ..$ wd        : num [1:8784] NA NA 130 NA NA 138 NA NA 120 NA ...
#>   ..$ ws        : num [1:8784] NA NA 1 NA NA 1.5 NA NA 2 NA ...
#>   ..$ atmos_pres: num [1:8784] NA NA 1034 NA NA ...
#>   ..$ dew_point : num [1:8784] NA NA -8.5 NA NA -6.7 NA NA -5.6 NA ...
#>   ..$ rh        : num [1:8784] NA NA 55.1 NA NA 62.9 NA NA 67 NA ...
#>   ..$ ceil_hgt  : num [1:8784] NA NA NA NA NA NA NA NA NA NA ...
#>   ..$ visibility: num [1:8784] NA NA 11000 NA NA 10400 NA NA 8000 NA ...
Created on 2021-08-14 by the reprex package (v2.0.0)

Hi HanOostdijk,

Thanks. It works.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.