I have a data frame as follows:
d <- data.frame(
shop=c('A','A','A','A','A','B','B','B','C','C','C','C','D','E','E'),
product=c(1:5,1:3,1:4,1,1:2),
price=c(16.83, 12.21, 9.99, 3.99, 1.00,
19.50, 6.42, 1.89,
13.95, 12.50, 7.87, 0.79,
11.99,
22.80, 15.99) )
The data represent the prices of products held by shops, with the products numbered in reverse price-order within each shop. I'd like to discover various things (e.g., mean, distribution, maximum price) about the most expensive products across stores, and similarly about the least expensive. With that in mind, I want to rescale the product numbering so that in all stores, the most expensive is numbered 1 and the least expensive is numbered 10 (with fractional product numbers being just fine).
Things I know:
- How to scale a single vector of product numbers using the maximum and minimum values
- How to obtain the maximum product number by shop:
aggregate(d[, 2], list(d$shop), max)
But I don't see how to fit the pieces together so that, for example, the product numbers for shop C
are, from most expensive to least expensive, c(1,4,7,10)
.