mapLevels.html
7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!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) <- 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 “levelsMap” or “listLevelsMap”
objects as described in levelsMap and listLevelsMap section.
<br>
Result of <code>mapLevels<-</code> is always a factor with remapped levels or
a “list/data.frame” with remapped factors.</p>
<h3>mapLevels</h3>
<p>
<code>mapLevels</code> function was written primarly for work with
“factors”, but is generic and can also be used with
“character”, “list” and “data.frame”, while
“default” method produces error. Here the term levels is also
used for unique character values.
</p>
<p>
When <code>codes=TRUE</code> <B>integer “levelsMap”</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
“levelsMap”</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<-</code> section and shown in examples.
</p>
<p>
<code>sort</code> argument provides possibility to sort levels of
“character” <code>x</code> and has no effect when <code>x</code> is a
“factor”.
</p>
<p>
Argument <code>combine</code> has effect only in “list” and
“data.frame” methods and when <code>codes=FALSE</code> i.e. with
<B>character “levelsMaps”</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 “list” and
“data.frame” 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
“levelsMap”, 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
“levelsMap”. <B>Integer “levelsMap”</B> (when
<code>codes=TRUE</code>) has names equal to levels and components equal to
internal codes. <B>Character “levelsMap”</B> (when
<code>codes=FALSE</code>) has names and components equal to levels. When
<code>mapLevels</code> is applied to “list” or “data.frame”,
result is of class “listLevelsMap”, which is a list of
“levelsMap” components described previously. If
<code>combine=TRUE</code>, result is a “levelsMap” with all levels in
<code>x</code> components.
</p>
<p>
For ease of inspection, print methods unlists “levelsMap” with
proper names. <code>mapLevels<-</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
“listLevelsMap” to “levelsMap”) 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 “levelsMap”.
</p>
<h3>mapLevels<-</h3>
<p>
Workhorse under <code>mapLevels<-</code> methods is
<code><a href="../../base/html/levels.html">levels<-</a></code>. <code>mapLevels<-</code> just control the assignment
of “levelsMap” (integer or character) or “listLevelsMap”
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
“levelsMap”</B> can be applied to “integer” or
“factor”, while <B>character “levelsMap”</B> can be
applied to “character” or “factor”. Methods for
“list” and “data.frame” can work only on mentioned atomic
components/columns and can accept either “levelsMap” or
“listLevelsMap”. Recycling occurs, if length of <code>value</code> is not
the same as number of components/columns of a “list/data.frame”.
</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 <- factor(sample(letters, size=20, replace=TRUE)))
(mapInt <- mapLevels(f))
## Integer to factor
(int <- as.integer(f))
(mapLevels(int) <- mapInt)
all.equal(int, f)
## Remap levels of a factor
(fac <- factor(as.integer(f)))
(mapLevels(fac) <- mapInt) # the same as levels(fac) <- mapInt
all.equal(fac, f)
## --- Character levelesMap ---
f1 <- factor(letters[1:10])
f2 <- 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 <- mapLevels(f1, codes=FALSE)
mapCha2 <- mapLevels(f2, codes=FALSE)
(mapCha <- c(mapCha1, mapCha2))
## Remap factors
mapLevels(f1) <- mapCha # the same as levels(f1) <- mapCha
mapLevels(f2) <- mapCha # the same as levels(f2) <- mapCha
## Internal codes are now "consistent" among factors
as.integer(f1)
as.integer(f2)
## Remap characters to get factors
f1 <- as.character(f1); f2 <- as.character(f2)
mapLevels(f1) <- mapCha
mapLevels(f2) <- 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>