Learn R Programming

memisc (version 0.99.31.8)

coarsen: Coarsen a vector into a factor with a lower number of levels

Description

coarsen can be used to obtain a factor from a vector, similar to cut, but with less technical and more "aesthetic" labels of the factor levels.

Usage

coarsen(x,...)
# S3 method for numeric
coarsen(x,
        n=5,
        pretty=TRUE,
        quantiles=!pretty,
        breaks=NULL,
        brackets=FALSE,
        sep=if(brackets)";"else if(quantiles) "-" else " - ",
        left="[",
        right="]",
        range=FALSE,
        labels=NULL,
        ...)

Arguments

x

a vector, usually a numeric vector

n

number of categories of the resulting factor

pretty

a logical value, whether pretty should be used to compute the breaks.

quantiles

a logical value, whether quantile should be used to compute the breaks.

breaks

a vector of break points or NULL.

brackets

a logical value, whether the labels should include brackets.

sep

a character string, used as a separator between upper and lower boundaries in the labels.

left

a character string, to be used as the left bracket

right

a character string, to be used as the right bracket

range

a logical value, whether the minimum and maximum of x should be included into breaks.

labels

an optional character vector of labels.

...

further arguments, passed on to pretty or quantile if applicable.

Examples

Run this code
x <- rnorm(200)

table(coarsen(x))

table(coarsen(x,quantiles=TRUE))

table(coarsen(x,brackets=TRUE))

table(coarsen(x,breaks=c(-1,0,1)))

table(coarsen(x,breaks=c(-1,0,1),
              range=TRUE,labels=letters[1:4]))

Run the code above in your browser using DataLab