ceff-tech / ffc_api_client

An R client for the online Functional Flows Calculator API
https://ceff-tech.github.io/ffc_api_client
9 stars 3 forks source link

Include number of observations as output in observed percentiles table #46

Open nickrsan opened 4 years ago

nickrsan commented 4 years ago

Ted wanted to know the number of observations for each metric that led to the calculated percentiles. These are different than the number of data available since it depends on the calculations coming out of the FFC. This should be relatively trivial to add into the percentile calculation code, but we'd then need to make sure to drop it anywhere else where we don't need it, but need to merge with the predicted percentiles. It could also come out as a separate table. Here's the code we used to calculate it as a oneoff, in case that's useful - it'd be good to switch the for loop to an apply, but had trouble since the structure of the ffc_results data meant that getting the name of each metric was not straightforward outside of a loop:

observation_number_func <- function(element){
  comid = element$predicted_percentiles$comid[1]
  gage_id = element$predicted_percentiles$gage_id[1]
  metrics <- as.data.frame(element$ffc_results)
  metric_results = data.frame("comid" = comid, "gage_id" = gage_id, "metric" = NA, num_observations = NA)
  for(column in colnames(metrics)){
    metric_results <- rbind(metric_results, data.frame("comid" = comid, "gage_id" = gage_id, "metric" = column, "num_observations" = sum(!is.na(metrics[column]))))
  }
  metric_results <- metric_results[!is.na(metric_results$metric), ]
  metric_results <- dplyr::filter(metric_results, !grepl("_Julian", metric))
  metric_results <- dplyr::filter(metric_results, !grepl("X__", metric))
  return(metric_results)
}

results <- lapply(usgs_ffc_alt, observation_number_func)
df_results <- do.call("rbind", results)