Learn R Programming

DataCombine (version 0.2.21)

change: Calculate the changes (absolute, percent, and proportion) changes from a specified lag, including within groups

Description

Calculate the changes (absolute, percent, and proportion) changes from a specified lag, including within groups

Usage

change(data, Var, GroupVar, TimeVar, NewVar, slideBy = -1, type = "percent", ...)

Arguments

data
a data frame object.
Var
a character string naming the variable you would like to find the percentage change for.
GroupVar
a character string naming the variable grouping the units within which the percentage change will be found for (i.e. countries in a time series). If GroupVar is missing then the entire data frame is treated as one unit.
TimeVar
optional character string naming the time variable. If specified then the data is ordered by Var-TimeVar before finding the change.
NewVar
a character string specifying the name for the new variable to place the percentage change in.
slideBy
numeric value specifying how many rows (time units) to make the percentage change comparison for. Positive values shift the data up--lead the data.
type
character string set at absolute, percent for percentages, or proportion to find proportions.
...
arguments passed to slide.

Value

a data frame

Details

Finds the absolute, percentage, or proportion change for over a given time period either within groups of data or the whole data frame. Important: the data must be in time order and, if groups are used, group-time order.

Examples

Run this code
# Create fake data frame
A <- c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2)
B <- c(1:10)
Data <- data.frame(A, B)

# Find percentage change from two periods before
Out <- change(Data, Var = 'B',
         type = 'proportion',
         NewVar = 'PercentChange',
         slideBy = -2)

Out

Run the code above in your browser using DataLab