Learn R Programming

piar (version 0.10.1)

back_period: Back period

Description

Offset a vector prices or quantities by computing the position one (or more) period backwards for each product.

Usage

back_period(period, product = NULL, match_first = TRUE, offset = 1L)

Value

A numeric vector of indices giving the position of the the back periods.

Arguments

period

[factor] A factor, or something that can be coerced into one, that gives the time period for each transaction. The ordering of time periods follows the levels of period to agree with cut().

product

[factor] A factor, or something that can be coerced into one, that gives the product identifier for each transaction. The default is to assume that all transactions are for the same product.

match_first

[logical(1)] Should products in the first period match with themselves (the default)?

offset

[integer(1)] The number of periods to offset. The default offsets by one period (back period). Setting to nlevels(period) gives the the base period.

See Also

outliers() for common methods to detect outliers for price relatives.

rs_pairs in the rsmatrix package for making sales pairs.

Examples

Run this code
prices <- data.frame(
  price = 1:6,
  product = factor(c("a", "b")),
  period = factor(c(1, 1, 2, 2, 3, 3))
)

with(prices, back_period(period, product))

# Make fixed-base price relatives.
with(
  prices,
  price / price[back_period(period, product, offset = nlevels(period))]
)

# Change the base period with relevel().
with(
  prices,
  price / price[
    back_period(relevel(period, "2"), product, offset = nlevels(period))
  ]
)

Run the code above in your browser using DataLab