[R-gregmisc-users] SF.net SVN: r-gregmisc: [983] trunk/gdata
Brought to you by:
warnes
|
From: <wa...@us...> - 2006-09-18 20:24:33
|
Revision: 983
http://svn.sourceforge.net/r-gregmisc/?rev=983&view=rev
Author: warnes
Date: 2006-09-18 13:24:18 -0700 (Mon, 18 Sep 2006)
Log Message:
-----------
Integrate fixes for trim() from Gregor and myself.
Modified Paths:
--------------
trunk/gdata/R/trim.R
trunk/gdata/inst/unitTests/Makefile
trunk/gdata/inst/unitTests/runit.trim.R
trunk/gdata/man/trim.Rd
trunk/gdata/tests/doRUnit.R
Modified: trunk/gdata/R/trim.R
===================================================================
--- trunk/gdata/R/trim.R 2006-09-18 19:32:26 UTC (rev 982)
+++ trunk/gdata/R/trim.R 2006-09-18 20:24:18 UTC (rev 983)
@@ -1,29 +1,30 @@
# $Id$
-trim <- function(s)
+trim <- function(s, recode.factor=TRUE)
UseMethod("trim", s)
-trim.default <- function(s)
+trim.default <- function(s, recode.factor=TRUE)
s
-trim.character <- function(s)
+trim.character <- function(s, recode.factor=TRUE)
{
s <- sub(pattern="^ +", replacement="", x=s)
s <- sub(pattern=" +$", replacement="", x=s)
s
}
-trim.factor <- function(s)
+trim.factor <- function(s, recode.factor=TRUE)
{
levels(s) <- trim(levels(s))
+ if(recode.factor) s <- reorder.factor(s, sort=sort)
s
}
-trim.list <- function(s)
- lapply(s, trim)
+trim.list <- function(s, recode.factor=TRUE)
+ lapply(s, trim, recode.factor=recode.factor)
-trim.data.frame <- function(s)
+trim.data.frame <- function(s, recode.factor=TRUE)
{
- s[] <- trim.list(s)
+ s[] <- trim.list(s, recode.factor=recode.factor)
s
}
Modified: trunk/gdata/inst/unitTests/Makefile
===================================================================
--- trunk/gdata/inst/unitTests/Makefile 2006-09-18 19:32:26 UTC (rev 982)
+++ trunk/gdata/inst/unitTests/Makefile 2006-09-18 20:24:18 UTC (rev 983)
@@ -1,14 +1,15 @@
PKG=gdata
TOP=../..
SUITE=doRUnit.R
+R=R
all: inst test
inst: # Install package
cd ${TOP}/..;\
- R CMD INSTALL ${PKG}
+ ${R} CMD INSTALL ${PKG}
test: # Run unit tests
export RCMDCHECK=FALSE;\
cd ${TOP}/tests;\
- R --vanilla --slave < ${SUITE}
+ ${R} --vanilla --slave < ${SUITE}
Modified: trunk/gdata/inst/unitTests/runit.trim.R
===================================================================
--- trunk/gdata/inst/unitTests/runit.trim.R 2006-09-18 19:32:26 UTC (rev 982)
+++ trunk/gdata/inst/unitTests/runit.trim.R 2006-09-18 20:24:18 UTC (rev 983)
@@ -20,8 +20,8 @@
sTrim <- " this is an example string "
sTrimR <- "this is an example string"
- fTrim <- c(sTrim, sTrim, " A", " B ", " C ", "D ")
- fTrimR <- c(sTrimR, sTrimR, "A", "B", "C", "D")
+ fTrim <- factor(c(sTrim, sTrim, " A", " B ", " C ", "D "))
+ fTrimR <- factor(c(sTrimR, sTrimR, "A", "B", "C", "D"))
lTrim <- list(s=rep(sTrim, times=6), f=fTrim, i=1:6)
lTrimR <- list(s=rep(sTrimR, times=6), f=fTrimR, i=1:6)
@@ -31,6 +31,10 @@
checkIdentical(trim(sTrim), sTrimR)
checkIdentical(trim(fTrim), fTrimR)
+ checkIdentical(
+ levels(trim(fTrim, recode.factor=FALSE)),
+ c("this is an example string", "C", "A", "B", "D")
+ )
checkIdentical(trim(lTrim), lTrimR)
checkIdentical(trim(dfTrim), dfTrimR)
}
Modified: trunk/gdata/man/trim.Rd
===================================================================
--- trunk/gdata/man/trim.Rd 2006-09-18 19:32:26 UTC (rev 982)
+++ trunk/gdata/man/trim.Rd 2006-09-18 20:24:18 UTC (rev 983)
@@ -2,13 +2,15 @@
\alias{trim}
\title{Remove leading and trailing spaces from character strings}
\description{
- Remove leading and traling spaces from character strings
+ Remove leading and trailing spaces from character strings and other
+ related objects.
}
\usage{
-trim(s)
+trim(s, recode.factor=TRUE)
}
\arguments{
\item{s}{object to be processed}
+ \item{recode.factor}{Should levels of a factor be recoded, see below}
}
\details{
@@ -18,24 +20,40 @@
factor \code{s} trims \code{\link{levels}}. There are also methods for
\code{list} and \code{data.frame}.
+Trimming character strings can change the sort order in some
+locales. For factors, this can affect the coding of levels. By
+default, factor levels are recoded to match the trimmed sort order, but
+this can be disabled by setting \code{recode.factor=FALSE}.
+
}
\value{
- \code{s} with all leading and traling spaces removed in its elements.
+ \code{s} with all leading and trailing spaces removed in its elements.
}
-\author{ Gregory R. Warnes \email{wa...@bs...} }
-\seealso{ \code{\link[base]{sub}}, \code{\link[base]{gsub}} }
+\author{ Gregory R. Warnes \email{wa...@bs...} with
+ contributions by Gregor Gorjanc}
+\seealso{ \code{\link[base]{sub}}, \code{\link[base]{gsub}} as well as
+ argument \code{strip.white} in \code{\link{read.table}}}
\examples{
s <- " this is an example string "
trim(s)
-f <- c(s, s, " A", " B ", " C ", "D ")
+f <- factor(c(s, s, " A", " B ", " C ", "D "))
+levels(f)
+
trim(f)
+levels(trim(f))
+trim(f,recode.factor=FALSE)
+levels(trim(f,recode.factor=FALSE))
+
+
l <- list(s=rep(s, times=6), f=f, i=1:6)
trim(l)
df <- as.data.frame(l)
trim(df)
+
}
+\keyword{manip}
\keyword{character}
Modified: trunk/gdata/tests/doRUnit.R
===================================================================
--- trunk/gdata/tests/doRUnit.R 2006-09-18 19:32:26 UTC (rev 982)
+++ trunk/gdata/tests/doRUnit.R 2006-09-18 20:24:18 UTC (rev 983)
@@ -1,8 +1,8 @@
-## doRUnit.R
+### doRUnit.R
###------------------------------------------------------------------------
-## What: Run RUnit tests
-## $Id$
-## Time-stamp: <2006-08-09 23:27:21 ggorjan>
+### What: Run RUnit tests
+### $Id$
+### Time-stamp: <2006-09-18 13:14:34 ggorjan>
###------------------------------------------------------------------------
if(require("RUnit", quietly=TRUE)) {
@@ -46,4 +46,4 @@
}
###------------------------------------------------------------------------
-## doRUnit.R ends here
+### doRUnit.R ends here
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|