rs_pairs {rsmatrix} | R Documentation |
Sales pairs
Description
Turn repeat-sales data into sales pairs that are suitable for making repeat-sales matrices.
Usage
rs_pairs(period, product)
Arguments
period |
A vector that gives the time period for each sale. Usually a
date vector, or a factor with the levels in chronological order, but other
values are possible if they can be sorted in chronological order (i.e., with
|
product |
A vector that gives the product identifier for each sale. Usually a factor or vector of integer codes for each product. |
Value
A numeric vector of indices giving the position of the previous sale
for each product
, with the convention that the previous sale for the
first sale is itself. Ties are resolved according to the order they
appear in period
.
Note
order()
is the workhorse of rs_pairs()
, so performance can be
sensitive to the types of period
and product
, and can be slow for large
character vectors.
See Also
rs_matrix()
for using sales pairs to make a repeat-sales index.
Examples
# Make sales pairs
x <- data.frame(
id = c(1, 1, 1, 3, 2, 2, 3, 3),
date = c(1, 2, 3, 2, 1, 3, 4, 1),
price = c(1, 3, 2, 3, 1, 1, 1, 2)
)
pairs <- rs_pairs(x$date, x$id)
x[c("date_prev", "price_prev")] <- x[c("date", "price")][pairs, ]
x