forked from STAT545-UBC/STAT545-UBC-original-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock014_factors.html
437 lines (412 loc) · 25.5 KB
/
block014_factors.html
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<title>Be the boss of your factors</title>
<script src="libs/jquery-1.11.0/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="libs/bootstrap-2.3.2/css/united.min.css" rel="stylesheet" />
<link href="libs/bootstrap-2.3.2/css/bootstrap-responsive.min.css" rel="stylesheet" />
<script src="libs/bootstrap-2.3.2/js/bootstrap.min.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<link rel="stylesheet"
href="libs/highlight/default.css"
type="text/css" />
<script src="libs/highlight/highlight.js"></script>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs && document.readyState && document.readyState === "complete") {
window.setTimeout(function() {
hljs.initHighlighting();
}, 0);
}
</script>
<link rel="stylesheet" href="libs/local/nav.css" type="text/css" />
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
</style>
<div class="container-fluid main-container">
<header>
<div class="nav">
<a class="nav-logo" href="index.html">
<img src="static/img/stat545-logo-s.png" width="70px" height="70px"/>
</a>
<ul>
<li class="home"><a href="index.html">Home</a></li>
<li class="faq"><a href="faq.html">FAQ</a></li>
<li class="syllabus"><a href="syllabus.html">Syllabus</a></li>
<li class="topics"><a href="topics.html">Topics</a></li>
<li class="people"><a href="people.html">People</a></li>
</ul>
</div>
</header>
<div id="header">
<h1 class="title">Be the boss of your factors</h1>
</div>
<div id="TOC">
<ul>
<li><a href="#load-the-gapminder-data">Load the Gapminder data</a></li>
<li><a href="#model-life-expectancy-as-a-function-of-year">Model life expectancy as a function of year</a></li>
<li><a href="#get-to-know-the-country-factor">Get to know the country factor</a></li>
<li><a href="#why-the-order-of-factor-levels-matters">Why the order of factor levels matters</a></li>
<li><a href="#droplevels-to-drop-unused-factor-levels"><code>droplevels()</code> to drop unused factor levels</a></li>
<li><a href="#reorder-to-reorder-factor-levels"><code>reorder()</code> to reorder factor levels</a></li>
<li><a href="#reorder-exercise"><code>reorder()</code> exercise</a></li>
<li><a href="#revaluing-factor-levels">Revaluing factor levels</a></li>
<li><a href="#grow-a-factor-object">Grow a factor object</a></li>
<li><a href="#to-add">TO ADD</a></li>
</ul>
</div>
<p><em>WARNING: STILL UNDER CONSTRUCTION. Here’s a <a href="http://stat545-ubc.github.io/block014_factors.html">link to some 2013 content</a> which has been partially ported. So far, it has more big picture and this has more Gapminder examples. Sort of.</em></p>
<div id="load-the-gapminder-data" class="section level3">
<h3>Load the Gapminder data</h3>
<p>As usual, load the Gapminder excerpt. Load the <code>plyr</code>, <code>dplyr</code> (<strong>in that order</strong>), and <code>ggplot2</code> packages.</p>
<pre class="r"><code>library(plyr)
suppressPackageStartupMessages(library(dplyr))
library(ggplot2)
gDat <- read.delim("gapminderDataFiveYear.txt")
str(gDat)
## 'data.frame': 1704 obs. of 6 variables:
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ pop : num 8425333 9240934 10267083 11537966 13079460 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ lifeExp : num 28.8 30.3 32 34 36.1 ...
## $ gdpPercap: num 779 821 853 836 740 ...
## or do this if the file isn't lying around already
## gd_url <- "http://tiny.cc/gapminder"
## gDat <- read.delim(gd_url)</code></pre>
</div>
<div id="model-life-expectancy-as-a-function-of-year" class="section level3">
<h3>Model life expectancy as a function of year</h3>
<p>For each country, retain estimated intercept and slope from a linear fit – regressing life expectancy on year. I include <code>country</code> AND <code>continent</code> in the factors on which to split, so that the <code>continent</code> factor appears in my result.</p>
<pre class="r"><code>j_coefs <- ddply(gDat, ~ country + continent, function(dat, offset = 1952) {
the_fit <- lm(lifeExp ~ I(year - offset), dat)
setNames(coef(the_fit), c("intercept", "slope"))
})</code></pre>
</div>
<div id="get-to-know-the-country-factor" class="section level3">
<h3>Get to know the country factor</h3>
<pre class="r"><code>str(j_coefs)
## 'data.frame': 142 obs. of 4 variables:
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 4 1 1 2 5 4 3 3 4 ...
## $ intercept: num 29.9 59.2 43.4 32.1 62.7 ...
## $ slope : num 0.275 0.335 0.569 0.209 0.232 ...
levels(j_coefs$country)
## [1] "Afghanistan" "Albania"
## [3] "Algeria" "Angola"
## [5] "Argentina" "Australia"
## [7] "Austria" "Bahrain"
## [9] "Bangladesh" "Belgium"
## [11] "Benin" "Bolivia"
## [13] "Bosnia and Herzegovina" "Botswana"
## [15] "Brazil" "Bulgaria"
## [17] "Burkina Faso" "Burundi"
## [19] "Cambodia" "Cameroon"
## [21] "Canada" "Central African Republic"
## [23] "Chad" "Chile"
## [25] "China" "Colombia"
## [27] "Comoros" "Congo, Dem. Rep."
## [29] "Congo, Rep." "Costa Rica"
## [31] "Cote d'Ivoire" "Croatia"
## [33] "Cuba" "Czech Republic"
## [35] "Denmark" "Djibouti"
## [37] "Dominican Republic" "Ecuador"
## [39] "Egypt" "El Salvador"
## [41] "Equatorial Guinea" "Eritrea"
## [43] "Ethiopia" "Finland"
## [45] "France" "Gabon"
## [47] "Gambia" "Germany"
## [49] "Ghana" "Greece"
## [51] "Guatemala" "Guinea"
## [53] "Guinea-Bissau" "Haiti"
## [55] "Honduras" "Hong Kong, China"
## [57] "Hungary" "Iceland"
## [59] "India" "Indonesia"
## [61] "Iran" "Iraq"
## [63] "Ireland" "Israel"
## [65] "Italy" "Jamaica"
## [67] "Japan" "Jordan"
## [69] "Kenya" "Korea, Dem. Rep."
## [71] "Korea, Rep." "Kuwait"
## [73] "Lebanon" "Lesotho"
## [75] "Liberia" "Libya"
## [77] "Madagascar" "Malawi"
## [79] "Malaysia" "Mali"
## [81] "Mauritania" "Mauritius"
## [83] "Mexico" "Mongolia"
## [85] "Montenegro" "Morocco"
## [87] "Mozambique" "Myanmar"
## [89] "Namibia" "Nepal"
## [91] "Netherlands" "New Zealand"
## [93] "Nicaragua" "Niger"
## [95] "Nigeria" "Norway"
## [97] "Oman" "Pakistan"
## [99] "Panama" "Paraguay"
## [101] "Peru" "Philippines"
## [103] "Poland" "Portugal"
## [105] "Puerto Rico" "Reunion"
## [107] "Romania" "Rwanda"
## [109] "Sao Tome and Principe" "Saudi Arabia"
## [111] "Senegal" "Serbia"
## [113] "Sierra Leone" "Singapore"
## [115] "Slovak Republic" "Slovenia"
## [117] "Somalia" "South Africa"
## [119] "Spain" "Sri Lanka"
## [121] "Sudan" "Swaziland"
## [123] "Sweden" "Switzerland"
## [125] "Syria" "Taiwan"
## [127] "Tanzania" "Thailand"
## [129] "Togo" "Trinidad and Tobago"
## [131] "Tunisia" "Turkey"
## [133] "Uganda" "United Kingdom"
## [135] "United States" "Uruguay"
## [137] "Venezuela" "Vietnam"
## [139] "West Bank and Gaza" "Yemen, Rep."
## [141] "Zambia" "Zimbabwe"
head(j_coefs$country)
## [1] Afghanistan Albania Algeria Angola Argentina Australia
## 142 Levels: Afghanistan Albania Algeria Angola Argentina ... Zimbabwe</code></pre>
<p>The levels are in alphabetical order. Why? Because. Just because. Do you have a better idea? THEN STEP UP AND DO SOMETHING ABOUT IT.</p>
</div>
<div id="why-the-order-of-factor-levels-matters" class="section level3">
<h3>Why the order of factor levels matters</h3>
<pre class="r"><code>ggplot(j_coefs, aes(x = slope, y = country)) + geom_point(size = 3)
ggplot(j_coefs, aes(x = slope, y = reorder(country, slope))) +
geom_point(size = 3)</code></pre>
<p><img src="block014_factors_files/figure-html/alpha-order-silly1.png" title="" alt="" width="49%" /><img src="block014_factors_files/figure-html/alpha-order-silly2.png" title="" alt="" width="49%" /></p>
<p>Which figure do you find easier to navigate? Which is more interesting? The unsorted, i.e. alphabetical, is an example of visual <a href="http://junkcharts.typepad.com/numbersruleyourworld/2014/09/dont-data-puke-says-avinash-kaushik.html">data puke</a>, because there is no effort to help the viewer learn anything from the plot, even though it is really easy to do so. At the very least, always consider sorting your factor levels in some principled way.</p>
<p>The same point generally applies to tables as well.</p>
<p>Exercise (part of <a href="hw05_factor-boss-files-out-in.html">HW05</a>): Consider <code>post_arrange</code>, <code>post_reorder</code>, and <code>post_both</code> as defined below. State how the objects differ and discuss the differences in terms of utility within an exploratory analysis. If I swapped out <code>arrange(country)</code> for <code>arrange(slope)</code>, would we get the same result? Do you have any preference for one arrange statement over the other?</p>
<pre class="r"><code>post_arrange <- j_coefs %>% arrange(slope)
post_reorder <- j_coefs %>%
mutate(country = reorder(country, slope))
post_both <- j_coefs %>%
mutate(country = reorder(country, slope)) %>%
arrange(country)</code></pre>
</div>
<div id="droplevels-to-drop-unused-factor-levels" class="section level3">
<h3><code>droplevels()</code> to drop unused factor levels</h3>
<p>Many demos will be clearer if we create a smaller dataset with just a few countries.</p>
<p>Let’s look at these five countries: Egypt, Haiti, Romania, Thailand, Venezuela.</p>
<pre class="r"><code>h_countries <- c("Egypt", "Haiti", "Romania", "Thailand", "Venezuela")
hDat <- gDat %>%
filter(country %in% h_countries)
hDat %>% str
## 'data.frame': 60 obs. of 6 variables:
## $ country : Factor w/ 142 levels "Afghanistan",..: 39 39 39 39 39 39 39 39 39 39 ...
## $ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ pop : num 22223309 25009741 28173309 31681188 34807417 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ lifeExp : num 41.9 44.4 47 49.3 51.1 ...
## $ gdpPercap: num 1419 1459 1693 1815 2024 ...</code></pre>
<p>Look at the <code>country</code> factor. Look at it hard.</p>
<pre class="r"><code>#table(hDat$country)
#levels(hDat$country)
nlevels(hDat$country)
## [1] 142</code></pre>
<p>Even though <code>hDat</code> contains data for only 5 countries, the other 137 countries remain as possible levels of the <code>country</code> factor. Sometimes this is exactly what you want but sometimes it’s not.</p>
<p>When you want to drop unused factor levels, use <code>droplevels()</code>.</p>
<pre class="r"><code>iDat <- hDat %>% droplevels ## of droplevels(hDat)
iDat %>% str
## 'data.frame': 60 obs. of 6 variables:
## $ country : Factor w/ 5 levels "Egypt","Haiti",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ pop : num 22223309 25009741 28173309 31681188 34807417 ...
## $ continent: Factor w/ 4 levels "Africa","Americas",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ lifeExp : num 41.9 44.4 47 49.3 51.1 ...
## $ gdpPercap: num 1419 1459 1693 1815 2024 ...
table(iDat$country)
##
## Egypt Haiti Romania Thailand Venezuela
## 12 12 12 12 12
levels(iDat$country)
## [1] "Egypt" "Haiti" "Romania" "Thailand" "Venezuela"
nlevels(iDat$country)
## [1] 5</code></pre>
</div>
<div id="reorder-to-reorder-factor-levels" class="section level3">
<h3><code>reorder()</code> to reorder factor levels</h3>
<p>Now that we have a more manageable set of 5 countries, let’s compute their max life expectancies, view them, and view life expectancy vs. year.</p>
<pre class="r"><code>i_le_max <- iDat %>%
group_by(country) %>%
summarize(max_le = max(lifeExp))
i_le_max
## Source: local data frame [5 x 2]
##
## country max_le
## 1 Egypt 71.338
## 2 Haiti 60.916
## 3 Romania 72.476
## 4 Thailand 70.616
## 5 Venezuela 73.747</code></pre>
<pre class="r"><code>ggplot(i_le_max, aes(x = country, y = max_le, group = 1)) +
geom_path() + geom_point(size = 3)
ggplot(iDat, aes(x = year, y = lifeExp, group = country)) +
geom_line(aes(color = country))</code></pre>
<p><img src="block014_factors_files/figure-html/factor-order-example-before1.png" title="" alt="" width="49%" /><img src="block014_factors_files/figure-html/factor-order-example-before2.png" title="" alt="" width="49%" /></p>
<p>Here’s a plot of the max life expectancies and a spaghetti plot of life expectancy over time. Notice how the first plot jumps around? Notice how the legend of the second plot is completely out of order with the data?</p>
<p>Use the function <code>reorder()</code> to change the order of factor levels. Read <a href="http://www.rdocumentation.org/packages/stats/functions/reorder.factor">its documentation</a>.</p>
<pre class="r"><code>reorder(your_factor, your_quant_var, your_summarization_function)</code></pre>
<p>Let’s reorder the country factor <strong>logically</strong>, in this case by maximum life expectancy. Even though <code>i_le_max</code> already holds these numbers, I’m going to enact the reordering with the “raw” data to illustrate more about the <code>reorder()</code> function.</p>
<pre class="r"><code>jDat <- iDat %>%
mutate(country = reorder(country, lifeExp, max))
data.frame(before = levels(iDat$country), after = levels(jDat$country))
## before after
## 1 Egypt Haiti
## 2 Haiti Thailand
## 3 Romania Egypt
## 4 Thailand Romania
## 5 Venezuela Venezuela
j_le_max <- i_le_max %>%
mutate(country = reorder(country, max_le))
j_le_max <- i_le_max %>%
mutate(country = factor(country, levels = levels(jDat$country)))</code></pre>
<p>Let’s revisit the two figures to see how much more natural they are.</p>
<pre class="r"><code>ggplot(j_le_max, aes(x = country, y = max_le, group = 1)) +
geom_line() + geom_point(size = 3)
ggplot(jDat, aes(x = year, y = lifeExp)) +
geom_line(aes(color = country)) +
guides(color = guide_legend(reverse = TRUE))</code></pre>
<p><img src="block014_factors_files/figure-html/factor-order-example-after1.png" title="" alt="" width="49%" /><img src="block014_factors_files/figure-html/factor-order-example-after2.png" title="" alt="" width="49%" /></p>
<p>Conclusion: Use <code>reorder()</code> to reorder a factor according to a quantitative variable. A simple call like this:</p>
<pre class="r"><code>reorder(your_factor, your_quant_var)</code></pre>
<p>implies that the summarization function will default to <code>mean()</code>. If that’s not what you want, specify your own summarization function. It could be built-in, such as <code>max()</code>, or could be written by you on-the-fly or in advance.</p>
<p>You can do this and alter your actual data (or a new copy thereof). Or you can do this reordering on-the-fly, i.e. in an actual plotting or tabulation call, leaving the underlying data untouched.</p>
</div>
<div id="reorder-exercise" class="section level3">
<h3><code>reorder()</code> exercise</h3>
<p>Reorder the <code>continent</code> factor, according to the estimated intercepts.</p>
<p>To review, here’s where to pick up the story:</p>
<pre class="r"><code>j_coefs <- ddply(gDat, ~ country + continent, function(dat, offset = 1952) {
the_fit <- lm(lifeExp ~ I(year - offset), dat)
setNames(coef(the_fit), c("intercept", "slope"))
})
head(j_coefs)
## country continent intercept slope
## 1 Afghanistan Asia 29.90729 0.2753287
## 2 Albania Europe 59.22913 0.3346832
## 3 Algeria Africa 43.37497 0.5692797
## 4 Angola Africa 32.12665 0.2093399
## 5 Argentina Americas 62.68844 0.2317084
## 6 Australia Oceania 68.40051 0.2277238</code></pre>
<p>The figure on the left gives a stripplot of estimate intecepts, by continent, with continent in alpabetical order. The line connects continent-specific averages of the intercepts (approx. equal to life expectancy in 1952). The figure on the right gives same plot after the continents have been reordered by average estimated intercept.</p>
<p><img src="block014_factors_files/figure-html/continent-reorder-exercise1.png" title="" alt="" width="49%" /><img src="block014_factors_files/figure-html/continent-reorder-exercise2.png" title="" alt="" width="49%" /></p>
<p>Write the <code>reorder()</code> statement to do this.</p>
</div>
<div id="revaluing-factor-levels" class="section level3">
<h3>Revaluing factor levels</h3>
<p>What if you want to recode factor levels? I usually use the <code>revalue()</code> function from <code>plyr</code>; sometime I use <code>plyr::mapvalues()</code> which is a bit more general. In the past I have also used the <code>recode()</code> function from the <code>car</code> package.</p>
<pre class="r"><code>k_countries <- c("Australia", "Korea, Dem. Rep.", "Korea, Rep.")
kDat <- gDat %>%
filter(country %in% k_countries & year > 2000) %>%
droplevels
kDat
## country year pop continent lifeExp gdpPercap
## 1 Australia 2002 19546792 Oceania 80.370 30687.755
## 2 Australia 2007 20434176 Oceania 81.235 34435.367
## 3 Korea, Dem. Rep. 2002 22215365 Asia 66.662 1646.758
## 4 Korea, Dem. Rep. 2007 23301725 Asia 67.297 1593.065
## 5 Korea, Rep. 2002 47969150 Asia 77.045 19233.988
## 6 Korea, Rep. 2007 49044790 Asia 78.623 23348.140
levels(kDat$country)
## [1] "Australia" "Korea, Dem. Rep." "Korea, Rep."
kDat <- kDat %>%
mutate(new_country = revalue(country,
c("Australia" = "Oz",
"Korea, Dem. Rep." = "North Korea",
"Korea, Rep." = "South Korea")))
data.frame(levels(kDat$country), levels(kDat$new_country))
## levels.kDat.country. levels.kDat.new_country.
## 1 Australia Oz
## 2 Korea, Dem. Rep. North Korea
## 3 Korea, Rep. South Korea
kDat
## country year pop continent lifeExp gdpPercap new_country
## 1 Australia 2002 19546792 Oceania 80.370 30687.755 Oz
## 2 Australia 2007 20434176 Oceania 81.235 34435.367 Oz
## 3 Korea, Dem. Rep. 2002 22215365 Asia 66.662 1646.758 North Korea
## 4 Korea, Dem. Rep. 2007 23301725 Asia 67.297 1593.065 North Korea
## 5 Korea, Rep. 2002 47969150 Asia 77.045 19233.988 South Korea
## 6 Korea, Rep. 2007 49044790 Asia 78.623 23348.140 South Korea</code></pre>
</div>
<div id="grow-a-factor-object" class="section level3">
<h3>Grow a factor object</h3>
<p>Try to avoid this. If you must <code>rbind()</code>ing data.frames works much better than <code>c()</code>ing vectors.</p>
<pre class="r"><code>usa <- gDat %>%
filter(country == "United States" & year > 2000) %>%
droplevels
mex <- gDat %>%
filter(country == "Mexico" & year > 2000) %>%
droplevels
str(usa)
## 'data.frame': 2 obs. of 6 variables:
## $ country : Factor w/ 1 level "United States": 1 1
## $ year : int 2002 2007
## $ pop : num 2.88e+08 3.01e+08
## $ continent: Factor w/ 1 level "Americas": 1 1
## $ lifeExp : num 77.3 78.2
## $ gdpPercap: num 39097 42952
str(mex)
## 'data.frame': 2 obs. of 6 variables:
## $ country : Factor w/ 1 level "Mexico": 1 1
## $ year : int 2002 2007
## $ pop : num 1.02e+08 1.09e+08
## $ continent: Factor w/ 1 level "Americas": 1 1
## $ lifeExp : num 74.9 76.2
## $ gdpPercap: num 10742 11978
usa_mex <- rbind(usa, mex)
str(usa_mex)
## 'data.frame': 4 obs. of 6 variables:
## $ country : Factor w/ 2 levels "United States",..: 1 1 2 2
## $ year : int 2002 2007 2002 2007
## $ pop : num 2.88e+08 3.01e+08 1.02e+08 1.09e+08
## $ continent: Factor w/ 1 level "Americas": 1 1 1 1
## $ lifeExp : num 77.3 78.2 74.9 76.2
## $ gdpPercap: num 39097 42952 10742 11978
(oops <- c(usa$country, mex$country))
## [1] 1 1 1 1
(yeah <- factor(c(levels(usa$country)[usa$country],
levels(mex$country)[mex$country])))
## [1] United States United States Mexico Mexico
## Levels: Mexico United States</code></pre>
<p>If you really want to catenate factors with different levels, you must first convert to their levels as character data, combine, then re-convert to factor.</p>
</div>
<div id="to-add" class="section level3">
<h3>TO ADD</h3>
<p>Here or in a separate document based on porting the <a href="http://stat545-ubc.github.io/block014_factors.html">2013 material</a>, discuss the explicit creation of a factor via <code>factor()</code>.</p>
</div>
<div class="footer">
This work is licensed under the <a href="http://creativecommons.org/licenses/by-nc/3.0/">CC BY-NC 3.0 Creative Commons License</a>.
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
$(document).ready(function () {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>