Create a Factor Variable Using the Quantiles of a Continuous Variable
Create a factor variable using the quantiles of a continous variable.
CutQ(x, breaks = quantile(x, seq(0, 1, by = 0.25), na.rm = TRUE), labels = NULL, na.rm = FALSE, ...)
x |
continous variable. |
breaks |
the breaks for creating groups. By default the quartiles will be used, say |
labels |
labels for the levels of the resulting category. By default, labels are defined as |
na.rm |
Boolean indicating whether missing values should be removed when computing quantiles. Defaults to TRUE. |
... |
Optional arguments passed to |
It properly handles cases where more than one quantile obtains the same value, as in the second example below. Note that in this case, there will be fewer generated factor levels than the specified number of quantile intervals.
Factor variable with one level for each quantile interval given by q
.
Gregory R. Warnes <greg@warnes.net>, some slight modifications Andri Signorell <andri@signorell.net>
# create example data x <- rnorm(1000) # cut into quartiles quartiles <- CutQ(x) table(quartiles) # cut into deciles deciles <- CutQ(x, breaks=10, labels=NULL) table(deciles) # show handling of 'tied' quantiles. x <- round(x) # discretize to create ties stem(x) # display the ties deciles <- CutQ(x, breaks=10) table(deciles) # note that there are only 5 groups (not 10) # due to duplicates
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.