# 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
Run the code above in your browser using DataLab