trim.html 2.87 KB
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>R: Remove leading and trailing spaces from character strings</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../../R.css">
</head><body>

<table width="100%" summary="page for trim {gdata}"><tr><td>trim {gdata}</td><td align="right">R Documentation</td></tr></table>
<h2>Remove leading and trailing spaces from character strings</h2>


<h3>Description</h3>

<p>
Remove leading and trailing spaces from character strings and other
related objects.
</p>


<h3>Usage</h3>

<pre>
trim(s, recode.factor=TRUE, ...)
</pre>


<h3>Arguments</h3>

<table summary="R argblock">
<tr valign="top"><td><code>s</code></td>
<td>
object to be processed</td></tr>
<tr valign="top"><td><code>recode.factor</code></td>
<td>
should levels of a factor be recoded, see below</td></tr>
<tr valign="top"><td><code>...</code></td>
<td>
arguments passed to other methods, currently only to
<code><a href="reorder.html">reorder.factor</a></code> for factors</td></tr>
</table>

<h3>Details</h3>

<p>
<code>trim</code> is a generic function, where default method does nothing,
while method for character <code>s</code> trims its elements and method for
factor <code>s</code> trims <code><a href="../../base/html/levels.html">levels</a></code>. There are also methods for
<code>list</code> and <code>data.frame</code>.
</p>
<p>
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</code>. Recoding is done with
<code><a href="reorder.html">reorder.factor</a></code>.
</p>


<h3>Value</h3>

<p>
<code>s</code> with all leading and trailing spaces removed in its elements.</p>

<h3>Author(s)</h3>

<p>
Gregory R. Warnes <a href="mailto:warnes@bst.rochester.edu">warnes@bst.rochester.edu</a>  with
contributions by Gregor Gorjanc
</p>


<h3>See Also</h3>

<p>
<code><a href="../../base/html/sub.html">sub</a></code>, <code><a href="../../base/html/gsub.html">gsub</a></code> as well as
argument <code>strip.white</code> in <code><a href="../../utils/html/read.table.html">read.table</a></code> and
<code><a href="reorder.html">reorder.factor</a></code>
</p>


<h3>Examples</h3>

<pre>
s &lt;- "    this is an example string    "
trim(s)

f &lt;- 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 &lt;- list(s=rep(s, times=6), f=f, i=1:6)
trim(l)

df &lt;- as.data.frame(l)
trim(df)
</pre>



<hr><div align="center">[Package <em>gdata</em> version 2.3.1 <a href="00Index.html">Index]</a></div>

</body></html>