[R-gregmisc-users] SF.net SVN: r-gregmisc:[1977] trunk/gdata
Brought to you by:
warnes
From: <wa...@us...> - 2015-04-28 04:41:42
|
Revision: 1977 http://sourceforge.net/p/r-gregmisc/code/1977 Author: warnes Date: 2015-04-28 04:41:39 +0000 (Tue, 28 Apr 2015) Log Message: ----------- If arguments 'X' or 'FUN' is supplied to reorder.factor(), mimic the behavior of stats::reorder.default() rather than trying to call it via NextMethod. Modified Paths: -------------- trunk/gdata/R/reorder.R trunk/gdata/man/reorder.Rd Modified: trunk/gdata/R/reorder.R =================================================================== --- trunk/gdata/R/reorder.R 2015-04-28 04:27:04 UTC (rev 1976) +++ trunk/gdata/R/reorder.R 2015-04-28 04:41:39 UTC (rev 1977) @@ -1,7 +1,3 @@ -# $Id$ - -# Reorder the levels of a factor. - reorder.factor <- function(x, X, FUN, @@ -13,17 +9,28 @@ constructor <- if (order) ordered else factor if(!missing(X) || !missing(FUN)) - return( NextMethod(x)) + { + if(missing(FUN)) FUN <- 'mean' - if (!missing(new.order)) + ## I would prefer to call stats::reorder.default directly, + ## but it exported from stats, so the relevant code is + ## replicated here: + ## --> + scores <- tapply(X = X, INDEX = x, FUN = FUN, ...) + ans <- (if (order) + ordered + else factor)(x, levels = names(sort(scores, na.last = TRUE))) + attr(ans, "scores") <- scores + ## <-- + return(ans) + } + else if (!missing(new.order)) { if (is.numeric(new.order)) new.order <- levels(x)[new.order] else new.order <- new.order } - else if (!missing(FUN)) - new.order <- names(sort(tapply(X, x, FUN, ...))) else new.order <- sort(levels(x)) Modified: trunk/gdata/man/reorder.Rd =================================================================== --- trunk/gdata/man/reorder.Rd 2015-04-28 04:27:04 UTC (rev 1976) +++ trunk/gdata/man/reorder.Rd 2015-04-28 04:41:39 UTC (rev 1977) @@ -38,23 +38,23 @@ The groups are then sorted by this value, and the resulting order is used for the new factor level names. - If \code{new.order} is provided: For a numeric vector, the new factor - level names are constructed by reordering the factor levels according - to the numeric values. For vectors, \code{new.order} gives the list of - new factor level names. In either case levels omitted from - \code{new.order} will become missing (\code{NA}) values. + If \code{new.order} is a numeric vector, the new factor level names + are constructed by reordering the factor levels according to the + numeric values. If \code{new.order} is a chraccter vector, + \code{new.order} gives the list of new factor level names. In either + case levels omitted from \code{new.order} will become missing + (\code{NA}) values. - If \code{sort} is provided (as it is by default): The new - factor level names are generated by applying the supplied function - to the existing factor level names. With \code{sort=mixedsort} the - factor levels are sorted so that combined numeric and character - strings are sorted in according to character rules on the character - sections (including ignoring case), and the numeric rules for the - numeric sections. See \code{\link[gtools]{mixedsort}} for details. + If \code{sort} is provided (as it is by default): The new factor level + names are generated by calling the function specified by \code{sort} + to the existing factor level \emph{names}. With \code{sort=mixedsort} + (the default) the factor levels are sorted so that combined numeric + and character strings are sorted in according to character rules on + the character sections (including ignoring case), and the numeric + rules for the numeric sections. See \code{\link[gtools]{mixedsort}} + for details. } -\value{ - A new factor with reordered levels -} +\value{ A new factor with reordered levels } \author{Gregory R. Warnes \email{gr...@wa...}} @@ -67,21 +67,21 @@ summary(trt) # Note that the levels are not in a meaningful order. - # Change the order to something useful - # default "mixedsort" ordering + # Change the order to something useful.. + # - default "mixedsort" ordering trt2 <- reorder(trt) summary(trt2) - # using indexes: + # - using indexes: trt3 <- reorder(trt, new.order=c(4, 2, 3, 1)) summary(trt3) - # using label names: + # - using label names: trt4 <- reorder(trt, new.order=c("PLACEBO", "300 MG", "600 MG", "1200 MG")) summary(trt4) - # using frequency + # - using frequency trt5 <- reorder(trt, X=as.numeric(trt), FUN=length) summary(trt5) - # drop out the '300 MG' level + # Drop out the '300 MG' level trt6 <- reorder(trt, new.order=c("PLACEBO", "600 MG", "1200 MG")) summary(trt6) } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |