change: Calculate changes over a given time period

View source: R/change.R

changeR Documentation

Calculate changes over a given time period

Description

Calculates either the absolute or percentage change in a given variable from a dataframe over a certain period of time.

Usage

change(df, var_name, date_col, base_date, second_date, type = "absolute")

Arguments

df

A dataframe.

var_name

The name of the variable to calculate the change for, as a string.

date_col

The name of the column containing time periods, as a string.

base_date

The base date to calculate the change between.

second_date

The second date to calculate the change between.

type

Specify either an "absolute" or a "percentage" change. Defaults to "absolute".

Value

A numeric value, if calculating absolute changes, otherwise a string for percentage changes.

Examples


df <- data.frame("yrqtr" = c("2020 Q1", "2020 Q2", "2020 Q3", "2020 Q4"),
"receipts" = seq(10, 40, 10))

change(df, "receipts", "yrqtr", "2020 Q2", "2020 Q4") # returns 20
change(df, "receipts", "yrqtr", "2020 Q2", "2020 Q4", type = "percentage") # returns "100%"


moj-analytical-services/mojrap documentation built on July 30, 2023, 4:43 p.m.