Learn R Programming

vcdExtra (version 0.6-3)

cutfac: Cut a Numeric Variable to a Factor

Description

cutfac acts like cut, dividing the range of x into intervals and coding the values in x according in which interval they fall. However, it gives nicer labels for the factor levels and by default chooses convenient breaks among the values based on deciles. It is particularly useful for plots in which one wants to make a numeric variable discrete for the purpose of getting boxplots, spinograms or mosaic plots.

Usage

cutfac(x, breaks = NULL, q = 10)

Arguments

x
a numeric vector which is to be converted to a factor by cutting
breaks
either a numeric vector of two or more unique cut points or a single number (greater than or equal to 2) giving the number of intervals into which x is to be cut.
q
the number of quantile groups used to define breaks, if that has not been specified.

Value

  • A factor corresponding to x is returned

Details

By default, cut chooses breaks by equal lengths of the range of x, whereas cutfac uses quantile to choose breaks of roughly equal count.

See Also

cut, quantile

Examples

Run this code
# countreg not yet on CRAN
if (require(countreg)) {
data("CrabSatellites", package = "countreg")

# jittered scatterplot
plot(jitter(satellites) ~ width, data=CrabSatellites,
  ylab="Number of satellites (jittered)", xlab="Carapace width",
  cex.lab=1.25)
with(CrabSatellites, lines(lowess(width, satellites), col="red", lwd=2))

# boxplot, using deciles
plot(satellites ~ cutfac(width), data=CrabSatellites,
     ylab="Number of satellites", xlab="Carapace width (deciles)")
}

Run the code above in your browser using DataLab