Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_flow_data() fails if any gage has no data #28

Open
allopole opened this issue Dec 9, 2021 · 0 comments
Open

get_flow_data() fails if any gage has no data #28

allopole opened this issue Dec 9, 2021 · 0 comments

Comments

@allopole
Copy link

allopole commented Dec 9, 2021

get_flow_data() will fail if readNWISdv() returns no rows for a site, because bind_rows() does not know how to handle the empty dataframes. Proposed fix: skip dataframes with zero rows before binding rows. See last 5 lines below for the code fix.

get_flow_data <- function (gages_df, start_date, end_date) 
{
  if (!is.data.frame(gages_df)) 
    stop("gage_df must be a data.frame")
  flow_data <- lapply(gages_df$site_no, function(x) {
    dataRetrieval::readNWISdv(siteNumber = x, parameterCd = "00060", 
                              startDate = start_date, endDate = end_date)
  })
  omit <- sapply(flow_data, function(x) {
    length(x)
  })
  flow_data <- lapply(flow_data[omit > 0], function(x) {
    x
  })
  flow_data <- lapply(flow_data, function(x) {
    colnames(x)[3] <- "date"
    colnames(x)[4] <- "discharge"
    return(x)
  })
  skip <- which(unlist(lapply(flow_data, function(x) nrow(x))) == 0)
  flow_data <- dplyr::bind_rows(flow_data[-skip]) %>% dplyr::select_(~site_no, 
                                                              ~date, ~discharge) %>% dplyr::filter_(~discharge > 0)
  return(flow_data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant