[R-gregmisc-users] SF.net SVN: r-gregmisc:[1748] trunk/gtools/R/mixedsort.R
Brought to you by:
warnes
From: <wa...@us...> - 2013-11-26 14:39:04
|
Revision: 1748 http://sourceforge.net/p/r-gregmisc/code/1748 Author: warnes Date: 2013-11-26 14:38:58 +0000 (Tue, 26 Nov 2013) Log Message: ----------- mixedorder() was failing to correctly handle numbers including decimals due to a faulty regular expression. Prior to the fix: > drr [1] "Dose 0.3 mg" "Dose 0.04 mg" "Dose 0.5 mg" > gtools::mixedsort(drr) [1] "Dose 0.3 mg" "Dose 0.04 mg" "Dose 0.5 mg" After the fix: > drr [1] "Dose 0.3 mg" "Dose 0.04 mg" "Dose 0.5 mg" > mixedsort(drr) [1] "Dose 0.04 mg" "Dose 0.3 mg" "Dose 0.5 mg" In addition, an optimization was added that checked if the input vector was numeric. If so, simply use the existing base::order function. Modified Paths: -------------- trunk/gtools/R/mixedsort.R Modified: trunk/gtools/R/mixedsort.R =================================================================== --- trunk/gtools/R/mixedsort.R 2013-11-18 16:06:26 UTC (rev 1747) +++ trunk/gtools/R/mixedsort.R 2013-11-26 14:38:58 UTC (rev 1748) @@ -13,7 +13,11 @@ return(NULL) else if(length(x)==1) return(1) - + + if( is.numeric(x) ) + return( order(x) ) + + delim="\\$\\@\\$" numeric <- function(x) @@ -26,16 +30,16 @@ suppressWarnings( ifelse(is.na(as.numeric(x)), toupper(x), NA) ) } - x <- as.character(x) which.nas <- which(is.na(x)) which.blanks <- which(x=="") if(length(which.blanks) >0) - x[ which.blanks ] <- -Inf + x[ which.blanks ] <- -Inf + if(length(which.nas) >0) - x[ which.nas ] <- Inf + x[ which.nas ] <- Inf #### # - Convert each character string into an vector containing single @@ -43,7 +47,7 @@ #### # find and mark numbers in the form of +1.23e+45.67 - delimited <- gsub("([+-]{0,1}[0-9]+([eE][\\+\\-]{0,1}[0-9]+){0,1})", + delimited <- gsub("([+-]{0,1}[0-9]+.{0,1}[0-9]*([eE][\\+\\-]{0,1}[0-9]+.{0,1}[0-9]*){0,1})", paste(delim,"\\1",delim,sep=""), x) # separate out numbers This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |