1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
% $Id: quantcut.Rd 1433 2010-05-01 22:03:03Z warnes $
%
\name{quantcut}
\alias{quantcut}
\title{ Create a Factor Variable Using the Quantiles of a Continuous Variable}
\description{
Create a factor variable using the quantiles of a continous variable.
}
\usage{
quantcut(x, q=seq(0,1,by=0.25), na.rm=TRUE, ...)
}
%- maybe also `usage' for other objects documented here.
\arguments{
\item{x}{ Continous variable. }
\item{q}{ Vector of quantiles used for creating groups. Defaults to
\code{seq(0, 1, by=0.25)}. See \code{\link{quantile}} for details. }
\item{na.rm}{ Boolean indicating whether missing values should be
removed when computing quantiles. Defaults to TRUE.}
\item{\dots}{ Optional arguments passed to \code{\link{cut}}. }
}
\details{
This function uses \code{\link{quantile}} to obtain the specified
quantiles of \code{x}, then calls \code{\link{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 given by \code{q}.
}
\author{Gregory R. Warnes \email{greg@warnes.net}}
\seealso{ \code{\link{cut}}, \code{\link{quantile}} }
\examples{
## create example data
\testonly{
set.seed(1234)
}
x <- rnorm(1000)
## cut into quartiles
quartiles <- quantcut( x )
table(quartiles)
## cut into deciles
deciles <- quantcut( x, seq(0,1,by=0.1) )
table(deciles)
## show handling of 'tied' quantiles.
x <- round(x) # discretize to create ties
stem(x) # display the ties
deciles <- quantcut( x, seq(0,1,by=0.1) )
table(deciles) # note that there are only 5 groups (not 10)
# due to duplicates
}
\keyword{ manip }
|