1\name{trim} 2\alias{trim} 3\title{Remove leading and trailing spaces from character strings} 4\description{ 5 Remove leading and trailing spaces from character strings and other 6 related objects. 7} 8\usage{ 9trim(s, recode.factor=TRUE, \dots) 10} 11\arguments{ 12 \item{s}{object to be processed} 13 \item{recode.factor}{should levels of a factor be recoded, see below} 14 \item{\dots}{arguments passed to other methods, currently only to 15 \code{\link{reorder.factor}} for factors} 16} 17 18\details{ 19 20\code{trim} is a generic function, where default method does nothing, 21while method for character \code{s} trims its elements and method for 22factor \code{s} trims \code{\link{levels}}. There are also methods for 23\code{list} and \code{data.frame}. 24 25Trimming character strings can change the sort order in some locales. 26For factors, this can affect the coding of levels. By default, factor 27levels are recoded to match the trimmed sort order, but this can be 28disabled by setting \code{recode.factor=FALSE}. Recoding is done with 29\code{\link{reorder.factor}}. 30 31} 32\value{ 33 \code{s} with all leading and trailing spaces removed in its elements. 34} 35\author{Gregory R. Warnes \email{greg@warnes.net} with 36 contributions by Gregor Gorjanc} 37\seealso{ \code{\link[base]{trimws}}, \code{\link[base]{sub}},\code{\link[base]{gsub}} 38as well as argument \code{strip.white} in \code{\link{read.table}} and 39 \code{\link{reorder.factor}} 40} 41\examples{ 42s <- " this is an example string " 43trim(s) 44 45f <- factor(c(s, s, " A", " B ", " C ", "D ")) 46levels(f) 47 48trim(f) 49levels(trim(f)) 50 51trim(f, recode.factor=FALSE) 52levels(trim(f, recode.factor=FALSE)) 53 54l <- list(s=rep(s, times=6), f=f, i=1:6) 55trim(l) 56 57df <- as.data.frame(l) 58trim(df) 59} 60\keyword{manip} 61\keyword{character} 62