Creates a factor variable using the quantiles of a continuous variable.
Arguments
- x
continuous variable
- breaks
cut points used to create groups. By default, quartiles are used. See
quantile()for details. A single integer specifies the intended number of groups; for example,breaks = 10creates deciles.- labels
labels for the levels of the resulting category. By default, labels are defined as
Q1,Q2, and so on. The argument is passed tocut(), solabels = FALSEreturns integer codes instead of a factor. When quantiles are tied, the levels are built from the observed interval bounds instead andlabelsis ignored - a warning is issued in that case.- na.rm
logical. Should missing values be removed before the quantiles are computed? Defaults to
FALSE. Note that the quantiles themselves are always taken withna.rm = TRUE; this argument decides whether the missing values are dropped from the returned factor as well.- ...
optional arguments passed to
cut()
Details
This function uses quantile() to obtain the specified quantiles
of x, then calls cut() to create a factor variable using
the intervals specified by these quantiles.
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.
See also
Other cut:
cut.integer(),
cutAge()
Examples
# create example data
set.seed(1234)
x <- rnorm(1000)
# cut into quartiles
quartiles <- cutQ(x)
table(quartiles)
#> quartiles
#> Q1 Q2 Q3 Q4
#> 250 250 250 250
# cut into deciles
deciles <- cutQ(x, breaks=10, labels=NULL)
table(deciles)
#> deciles
#> Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10
#> 100 100 100 100 100 100 100 100 100 100
# show handling of 'tied' quantiles.
x <- round(x) # discretize to create ties
stem(x) # display the ties
#>
#> The decimal point is at the |
#>
#> -3 | 0000000000000
#> -2 |
#> -2 | 000000000000000000000000000000000000000000000000
#> -1 |
#> -1 | 00000000000000000000000000000000000000000000000000000000000000000000+181
#> -0 |
#> -0 |
#> 0 | 00000000000000000000000000000000000000000000000000000000000000000000+310
#> 0 |
#> 1 | 00000000000000000000000000000000000000000000000000000000000000000000+140
#> 1 |
#> 2 | 000000000000000000000000000000000000000000000000000000000000000
#> 2 |
#> 3 | 00000
#>
deciles <- cutQ(x, breaks=10)
table(deciles) # note that there are only 5 groups (not 10)
#> deciles
#> [-3,-1) -1 0 1 (1,3]
#> 61 261 390 220 68
# due to duplicates
