[R-gregmisc-users] SF.net SVN: r-gregmisc:[1949] trunk/gtools
Brought to you by:
warnes
From: <wa...@us...> - 2015-04-23 21:47:51
|
Revision: 1949 http://sourceforge.net/p/r-gregmisc/code/1949 Author: warnes Date: 2015-04-23 21:47:48 +0000 (Thu, 23 Apr 2015) Log Message: ----------- - The 'q' argument to quantcut()'s 'q' now accept an integer indicating the number of equally spaced quantile groups to create. (Suggestion and patch submitted by Ryan C. Thompson.) Modified Paths: -------------- trunk/gtools/R/quantcut.R trunk/gtools/man/quantcut.Rd Modified: trunk/gtools/R/quantcut.R =================================================================== --- trunk/gtools/R/quantcut.R 2015-04-23 21:23:36 UTC (rev 1948) +++ trunk/gtools/R/quantcut.R 2015-04-23 21:47:48 UTC (rev 1949) @@ -1,7 +1,10 @@ # $Id$ -quantcut <- function(x, q=seq(0,1,by=0.25), na.rm=TRUE, ... ) +quantcut <- function(x, q=4, na.rm=TRUE, ... ) { + if(length(q)==1) + q <- seq(0,1, length.out=q+1) + quant <- quantile(x, q, na.rm=na.rm) dups <- duplicated(quant) if(any(dups)) Modified: trunk/gtools/man/quantcut.Rd =================================================================== --- trunk/gtools/man/quantcut.Rd 2015-04-23 21:23:36 UTC (rev 1948) +++ trunk/gtools/man/quantcut.Rd 2015-04-23 21:47:48 UTC (rev 1949) @@ -13,8 +13,10 @@ %- 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{q}{ Either a integer number of equally spaced quantile groups to + create, or a vector of quantiles used for creating groups. Defaults to + \code{q=4} which is equivalent to \code{q=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}}. } @@ -31,7 +33,7 @@ of quantile intervals. } \value{ - Factor variable with one level for each quantile interval given by \code{q}. + Factor variable with one level for each quantile interval. } \author{Gregory R. Warnes \email{gr...@wa...}} @@ -51,13 +53,19 @@ table(quartiles) ## cut into deciles - deciles <- quantcut( x, seq(0,1,by=0.1) ) - table(deciles) + deciles.1 <- quantcut( x, 10 ) + table(deciles.1) + # or equivalently + deciles.2 <- quantcut( x, seq(0,1,by=0.1) ) + \testonly{ + stopifnot(identical(deciles.1, deciles.2)) + } + ## 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) ) + deciles <- quantcut( x, 10 ) table(deciles) # note that there are only 5 groups (not 10) # due to duplicates This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |