R/change.R

#' Calculates the change in a given variable over a given period
#' @param df CREST dataset
#' @param x A variable (disposals, receipts or outstanding).
#' @param y A number greater than 0. This is the number of quarters ago,
#' that you want to calculate change from.
#' @param z Absolute or percentage change.
#' @export

change <- function(df, x, y, z = "absolute"){
  (
    (df %>%
     filter(variable == x) %>%
     filter(row_number() == n()) %>%
     .$value) -
    (df %>%
       filter(variable == x) %>%
       filter(row_number() == n()-y) %>%
       .$value)
    )/
    if(z == "absolute"){
      1
      } else if(z== "percentage"){
        (df %>%
         filter(variable == x) %>%
         filter(row_number() == n()-y) %>%
         .$value/100)
    }
     }
moj-analytical-services/kpow2 documentation built on May 31, 2019, 5:03 a.m.