| ratios | R Documentation |
Calculation of regional price ratios per product with flexible setting of base prices.
ratios(p, r, n, base=NULL, static=FALSE, settings=list())
p |
A numeric vector of prices. |
r, n |
A character vector or factor of regional entities |
base |
A character specifying the base region to be used for the calculation of price ratios, i.e., |
static |
A logical indicating whether the |
settings |
A list of control settings to be used. The following settings are supported:
|
If base is not available for a specific product, n, and static=FALSE, another base region is used instead. This is particularly important in cases of missing prices. Otherwise, for static=TRUE, computation is not possible and gives NA.
If there are duplicated observations, only one of these duplicates is used as the base price. For example, if two prices are available for product n in base region r, ratios() divides both prices by the first one.
A numeric vector of the same length as p. If base has been adjusted for some products, the attribute attr("base") is added to the output, providing the respective base region.
Sebastian Weinand
### (1) unique price observations; no missings
set.seed(123)
dt1 <- rdata(R=3, B=1, N=4)
levels(dt1$region) <- c("a","b","c")
# calculate price ratios by product:
dt1[, ratios(p=price, r=region, n=product, base="b")]
### (2) unique price observations; missings
# drop two observations:
dt2 <- dt1[-c(5,10), ]
# now, region 'a' is base for product 2:
(pr <- dt2[, ratios(p=price, r=region, n=product, base="b")])
# base region prices are stored in attributes:
attr(pr, "base")
# with static base, NAs are produced:
dt2[, ratios(p=price, r=region, n=product, base="b", static=TRUE)]
### (3) treatment of duplicates and missing prices (not NAs):
# insert duplicates and missings:
dt3 <- rbind(dt1[2,], dt1[-c(1,10),])
dt3[1, "price" := dt1[2,price]+abs(rnorm(1))]
anyDuplicated(dt3, by=c("region","product"))
# duplicated prices are divided by the first base price:
dt3[, ratios(p=price, r=region, n=product, base="b")]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.