Calculates the rate of change of a value, provided a time variable. time and units should be compatible with the lubridate package

get_rate(x, time, unit = "days", historic = "backward")

Arguments

x

value to calculate the rate of

time

date or time variable to be used in the calculations of rate

unit

units of time to be used in output

historic

accepts "backward", "forward" or "average" as possible values. "backward" means look backwards for the rate, in this case the first observation will have a rate of NA. Similarly, "forward" means look forward and so the last observation will have a rate of NA. "average" will take the average of these two measurements and return no NAs

Examples

x <- c(4, 2, 1, 4, 6, 7, 4, 2, 2, 9) time <- lubridate::ymd(c( "2000-08-23", "2000-04-19", "2000-10-12", "2000-07-22", "2000-12-13", "2000-06-20", "2000-01-02", "2000-11-12", "2000-03-11", "2000-08-04" )) get_rate(x, time)
#> [1] -0.26315789 0.00000000 -0.06000000 -0.09375000 0.12903226 0.08064516 #> [7] NA 0.03225806 -0.02898551 0.38461538
if (FALSE) { df <- tibble::tibble(x, time) %>% dplyr::mutate(x.rate = get_rate(x, time, historic = "average")) }