Create a Factor Variable Using the Quantiles of a Continuous Variable

Share:

Description

Create a factor variable using the quantiles of a continous variable.

Usage

1
quantcut(x, q=4, na.rm=TRUE, ...)

Arguments

x

Continous variable.

q

Either a integer number of equally spaced quantile groups to create, or a vector of quantiles used for creating groups. Defaults to q=4 which is equivalent to q=seq(0, 1, by=0.25). See quantile for details.

na.rm

Boolean indicating whether missing values should be removed when computing quantiles. Defaults to TRUE.

...

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.

Value

Factor variable with one level for each quantile interval.

Author(s)

Gregory R. Warnes greg@warnes.net

See Also

cut, quantile

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  ## create example data
  
  x <- rnorm(1000)

  ## cut into quartiles
  quartiles <- quantcut( x )
  table(quartiles)

  ## cut into deciles
  deciles.1 <- quantcut( x, 10 )
  table(deciles.1)
  # or equivalently
  deciles.2 <- quantcut( x, seq(0,1,by=0.1) )

  

  ## show handling of 'tied' quantiles.
  x <- round(x)  # discretize to create ties
  stem(x)        # display the ties
  deciles <- quantcut( x, 10 )

  table(deciles) # note that there are only 5 groups (not 10)
                 # due to duplicates

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.