Rev 25360 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{cut}\title{Convert Numeric to Factor}\usage{cut(x, \dots)\method{cut}{default}(x, breaks, labels = NULL,include.lowest = FALSE, right = TRUE, dig.lab = 3, \dots)}\alias{cut}\alias{cut.default}\arguments{\item{x}{a numeric vector which is to be converted to a factor by cutting.}\item{breaks}{either a vector of cut points or numbergiving the number of intervals which \code{x} is to be cut into.}\item{labels}{labels for the levels of the resulting category. By default,labels are constructed using \code{"(a,b]"} interval notation. If\code{labels = FALSE}, simple integer codes are returned instead ofa factor.}.\item{include.lowest}{logical, indicating if an \sQuote{x[i]} equal tothe lowest (or highest, for \code{right = FALSE}) \sQuote{breaks}value should be included.}\item{right}{logical, indicating if the intervals should be closed onthe right (and open on the left) or vice versa.}\item{dig.lab}{integer which is used when labels are not given. Itdetermines the number of digits used in formatting the break numbers.}\item{\dots}{further arguments passed to or from other methods.}}\description{\code{cut} divides the range of \code{x} into intervalsand codes the values in \code{x} according to whichinterval they fall.The leftmost interval corresponds to level one,the next leftmost to level two and so on.}\value{A \code{\link{factor}} is returned, unless \code{labels = FALSE} whichresults in the mere integer level codes.}\details{If a \code{labels} parameter is specified, its values are usedto name the factor levels. If none is specified, the factorlevel labels are constructed as \code{"(b1, b2]"}, \code{"(b2, b3]"}etc. for \code{right=TRUE} and as \code{"[b1, b2)"}, \ldots if\code{right=FALSE}.In this case, \code{dig.lab} indicates how many digits should be used informatting the numbers \code{b1}, \code{b2}, \ldots.}\note{Instead of \code{table(cut(x, br))}, \code{hist(x, br, plot = FALSE)} ismore efficient and less memory hungry. Instead of \code{cut(*,labels = FALSE)}, \code{\link{findInterval}()} is more efficient.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth \& Brooks/Cole.}\seealso{\code{\link{split}} for splitting a variable according to a group factor;\code{\link{factor}}, \code{\link{tabulate}}, \code{\link{table}},\code{\link{findInterval}()}.}\examples{Z <- rnorm(10000)table(cut(Z, br = -6:6))sum(table(cut(Z, br = -6:6, labels=FALSE)))sum( hist (Z, br = -6:6, plot=FALSE)$counts)cut(rep(1,5),4)#-- dummytx0 <- c(9, 4, 6, 5, 3, 10, 5, 3, 5)x <- rep(0:8, tx0)stopifnot(table(x) == tx0)table( cut(x, b = 8))table( cut(x, br = 3*(-2:5)))table( cut(x, br = 3*(-2:5), right = FALSE))##--- some values OUTSIDE the breaks :table(cx <- cut(x, br = 2*(0:4)))table(cxl <- cut(x, br = 2*(0:4), right = FALSE))which(is.na(cx)); x[is.na(cx)] #-- the first 9 values 0which(is.na(cxl)); x[is.na(cxl)] #-- the last 5 values 8## Label construction:y <- rnorm(100)table(cut(y, breaks = pi/3*(-3:3)))table(cut(y, breaks = pi/3*(-3:3), dig.lab=4))table(cut(y, breaks = 1*(-3:3), dig.lab=4))# extra digits don't "harm" heretable(cut(y, breaks = 1*(-3:3), right = FALSE))#- the same, since no exact INT!}\keyword{category}