change | R Documentation |
Calculates either the absolute or percentage change in a given variable from a dataframe over a certain period of time.
change(df, var_name, date_col, base_date, second_date, type = "absolute")
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". |
A numeric value, if calculating absolute changes, otherwise a string for percentage changes.
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%"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.