R/fuel_type.R

Defines functions update_fuel_type

Documented in update_fuel_type

#' Impute Misssing GSA Fuel Values
#'
#' @param fuel_data a data frame containing the GSA fuel report.
#' @param inv_data a data frame containing the current inventory by LOB.
#' @param miles numeric. The cutoff milage for flex fuel required purchase.
#' @param ...
#'
#' @return a data frame. The GSA fuel report with missing fuel values imputed.
#' @export
#'
update_fuel_type <- function(fuel_data, inv_data, miles = 10, ...) {
    require(dplyr)
    clean_zip <- makeClean(5)

    missing_fuel <- gsa %>%
        filter(is.na(`Purchased Fuel Type`)) %>%
        filter(`Vehicle Fuel Type` == "E85") %>%
        select(Tag, `Vehicle Fuel Type`) %>%
        left_join(inv, by = c("Tag" = "VEHICLE TAG")) %>%
        select("Tag", "GARAGED ZIP") %>%
        mutate(`GARAGED ZIP` = clean_zip(`GARAGED ZIP`))

    dist <- purrr::map_dbl(missing_fuel, dist_to_e85)
    out <- missing_fuel %>%
        mutate(distance = dist,
               fuel_type = if_else(distance < miles, "E85", "GAS")) %>%
        select(Tag, fuel_type)

    fuel_data %>%
        mutate(`Purchased Fuel Type` = if_else(Tag %in% out$Tag,
                                               out[match(Tag, out$Tag), "fuel_type"],
                                               `Purchased Fuel Type`))
}
burch-cm/fleetreportr documentation built on Nov. 4, 2019, 8:20 a.m.