mapLevels.html 7.97 KB
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>R: Mapping levels</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 mapLevels {gdata}"><tr><td>mapLevels {gdata}</td><td align="right">R Documentation</td></tr></table>
<h2>Mapping levels</h2>


<h3>Description</h3>

<p>
<code>mapLevels</code> produces a map with information on levels and/or
internal integer codes. As such can be conveniently used to store level
mapping when one needs to work with internal codes of a factor and later
transfrorm back to factor or when working with several factors that
should have the same levels and therefore the same internal coding.
</p>


<h3>Usage</h3>

<pre>

mapLevels(x, codes=TRUE, sort=TRUE, drop=FALSE, combine=FALSE, ...)
mapLevels(x) &lt;- value

</pre>


<h3>Arguments</h3>

<table summary="R argblock">
<tr valign="top"><td><code>x</code></td>
<td>
object whose levels will be mapped, look into details</td></tr>
<tr valign="top"><td><code>codes</code></td>
<td>
boolean, create integer levelsMap (with internal
codes) or character levelsMap (with level names)</td></tr>
<tr valign="top"><td><code>sort</code></td>
<td>
boolean, sort levels of character <code>x</code>, look into
details</td></tr>
<tr valign="top"><td><code>drop</code></td>
<td>
boolean, drop unused levels</td></tr>
<tr valign="top"><td><code>combine</code></td>
<td>
boolean, combine levels, look into details</td></tr>
<tr valign="top"><td><code>...</code></td>
<td>
additional arguments for <code>sort</code></td></tr>
<tr valign="top"><td><code>value</code></td>
<td>
levelsMap or listLevelsMap, output of <code>mapLevels</code>
methods or constructed by user, look into details</td></tr>
</table>

<h3>Value</h3>

<p>
<code>mapLevels()</code> returns &ldquo;levelsMap&rdquo; or &ldquo;listLevelsMap&rdquo;
objects as described in levelsMap and listLevelsMap section.
<br>
Result of <code>mapLevels&lt;-</code> is always a factor with remapped levels or
a &ldquo;list/data.frame&rdquo; with remapped factors.</p>

<h3>mapLevels</h3>

<p>
<code>mapLevels</code> function was written primarly for work with
&ldquo;factors&rdquo;, but is generic and can also be used with
&ldquo;character&rdquo;, &ldquo;list&rdquo; and &ldquo;data.frame&rdquo;, while
&ldquo;default&rdquo; method produces error. Here the term levels is also
used for unique character values.
</p>
<p>
When <code>codes=TRUE</code> <B>integer &ldquo;levelsMap&rdquo;</B> with
information on mapping internal codes with levels is produced. Output
can be used to transform integer to factor or remap factor levels as
described bellow. With <code>codes=FALSE</code> <B>character
&ldquo;levelsMap&rdquo;</B> is produced. The later is usefull, when one would
like to remap factors or combine factors with some overlap in levels as
described in <code>mapLevels&lt;-</code> section and shown in examples.
</p>
<p>
<code>sort</code> argument provides possibility to sort levels of
&ldquo;character&rdquo; <code>x</code> and has no effect when <code>x</code> is a
&ldquo;factor&rdquo;.
</p>
<p>
Argument <code>combine</code> has effect only in &ldquo;list&rdquo; and
&ldquo;data.frame&rdquo; methods and when <code>codes=FALSE</code> i.e. with
<B>character &ldquo;levelsMaps&rdquo;</B>. The later condition is necesarry
as it is not possible to combine maps with different mapping of level
names and integer codes. It is assumed that passed &ldquo;list&rdquo; and
&ldquo;data.frame&rdquo; have all components for which methods
exist. Otherwise error is produced.
</p>


<h3>levelsMap and listLevelsMap</h3>

<p>
Function <code>mapLevels</code> returns a map of levels. This map is of class
&ldquo;levelsMap&rdquo;, which is actually a list of length equal to number
of levels and with each component of length 1. Components need not be of
length 1. There can be either integer or character
&ldquo;levelsMap&rdquo;. <B>Integer &ldquo;levelsMap&rdquo;</B> (when
<code>codes=TRUE</code>) has names equal to levels and components equal to
internal codes. <B>Character &ldquo;levelsMap&rdquo;</B> (when
<code>codes=FALSE</code>) has names and components equal to levels. When
<code>mapLevels</code> is applied to &ldquo;list&rdquo; or &ldquo;data.frame&rdquo;,
result is of class &ldquo;listLevelsMap&rdquo;, which is a list of
&ldquo;levelsMap&rdquo; components described previously. If
<code>combine=TRUE</code>, result is a &ldquo;levelsMap&rdquo; with all levels in
<code>x</code> components.
</p>
<p>
For ease of inspection, print methods unlists &ldquo;levelsMap&rdquo; with
proper names. <code>mapLevels&lt;-</code> methods are fairly general and
therefore additional convenience methods are implemented to ease the
work with maps: <code>is.levelsMap</code> and <code>is.listLevelsMap</code>;
<code>as.levelsMap</code> and <code>as.listLevelsMap</code> for coercion of user
defined maps; generic <code>"["</code> and <code>c</code> for both classes (argument
<code>recursive</code> can be used in <code>c</code> to coerce
&ldquo;listLevelsMap&rdquo; to &ldquo;levelsMap&rdquo;) and generic <code>unique</code>
and <code>sort</code> (generic from <font face="Courier New,Courier" color="#666666"><b>R</b></font> 2.4) for &ldquo;levelsMap&rdquo;.
</p>


<h3>mapLevels&lt;-</h3>

<p>
Workhorse under <code>mapLevels&lt;-</code> methods is
<code><a href="../../base/html/levels.html">levels&lt;-</a></code>. <code>mapLevels&lt;-</code> just control the assignment
of &ldquo;levelsMap&rdquo; (integer or character) or &ldquo;listLevelsMap&rdquo;
to <code>x</code>. The idea is that map values are changed to map names as
indicated in <code><a href="../../base/html/levels.html">levels</a></code> examples. <B>Integer
&ldquo;levelsMap&rdquo;</B> can be applied to &ldquo;integer&rdquo; or
&ldquo;factor&rdquo;, while <B>character &ldquo;levelsMap&rdquo;</B> can be
applied to &ldquo;character&rdquo; or &ldquo;factor&rdquo;. Methods for
&ldquo;list&rdquo; and &ldquo;data.frame&rdquo; can work only on mentioned atomic
components/columns and can accept either &ldquo;levelsMap&rdquo; or
&ldquo;listLevelsMap&rdquo;. Recycling occurs, if length of <code>value</code> is not
the same as number of components/columns of a &ldquo;list/data.frame&rdquo;.
</p>


<h3>Author(s)</h3>

<p>
Gregor Gorjanc
</p>


<h3>See Also</h3>

<p>
<code><a href="../../base/html/factor.html">factor</a></code>, <code><a href="../../base/html/levels.html">levels</a></code> and <code><a href="../../base/html/class.html">unclass</a></code>
</p>


<h3>Examples</h3>

<pre>

## --- Integer levelsMap ---

(f &lt;- factor(sample(letters, size=20, replace=TRUE)))
(mapInt &lt;- mapLevels(f))

## Integer to factor
(int &lt;- as.integer(f))
(mapLevels(int) &lt;- mapInt)
all.equal(int, f)

## Remap levels of a factor
(fac &lt;- factor(as.integer(f)))
(mapLevels(fac) &lt;- mapInt) # the same as levels(fac) &lt;- mapInt
all.equal(fac, f)

## --- Character levelesMap ---

f1 &lt;- factor(letters[1:10])
f2 &lt;- factor(letters[5:14])

## Internal codes are the same, but levels are not
as.integer(f1)
as.integer(f2)

## Get character levelsMaps and combine them
mapCha1 &lt;- mapLevels(f1, codes=FALSE)
mapCha2 &lt;- mapLevels(f2, codes=FALSE)
(mapCha &lt;- c(mapCha1, mapCha2))

## Remap factors
mapLevels(f1) &lt;- mapCha # the same as levels(f1) &lt;- mapCha
mapLevels(f2) &lt;- mapCha # the same as levels(f2) &lt;- mapCha

## Internal codes are now "consistent" among factors
as.integer(f1)
as.integer(f2)

## Remap characters to get factors
f1 &lt;- as.character(f1); f2 &lt;- as.character(f2)
mapLevels(f1) &lt;- mapCha
mapLevels(f2) &lt;- mapCha

## Internal codes are now "consistent" among factors
as.integer(f1)
as.integer(f2)

</pre>



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

</body></html>