How to create a column with a quartile rank?
A data.table approach
library(data.table)
tableOne <- setDT(tableOne)[, quartile := cut(salesPrice, quantile(salesPrice, probs=0:4/4), include.lowest=TRUE, labels=FALSE)]
This should do it:
tableOne <- within(tableOne, quartile <- as.integer(cut(salesPrice, quantile(salesPrice, probs=0:4/4), include.lowest=TRUE)))
...Some details:
The within
function is great for calculating new columns. You don't have to refer to columns as
tableOne$salesPrice
etc.
tableOne <- within(tableOne, quartile <- <<<some expression>>>)
The quantile
function calculates the quantiles (or in your case, quartiles). 0:4/4
evaluates to c(0, 0.25, 0.50, 0.75, 1)
.
Finally the cut
function splits your data into those quartiles. But you get a factor
with weird names, so as.integer
turns it into groups 1,2,3,4
.
Try ?within
etc to learn more about the functions mentioned here...