jenny — Sep 20, 2013, 3:41 PM
## Minimal example demonstrating Warning fiasco re: `reorder.factor()` from gdata
## verify a fresh clean search path
search()
[1] ".GlobalEnv" "package:knitr" "package:stats"
[4] "package:graphics" "package:grDevices" "package:utils"
[7] "package:datasets" "package:methods" "Autoloads"
[10] "package:base"
## tiny excerpt from some Gapminder data I teach with
foo <- data.frame(country = c("China", "Cuba", "Gambia", "Mali", "Norway"),
continent = c("Asia", "Americas", "Africa", "Africa", "Europe"),
intercept = c(47.2, 62.2, 28.4, 33.1, 72.2),
slope = c(0.53, 0.32, 0.58, 0.38, 0.13))
foo
country continent intercept slope
1 China Asia 47.2 0.53
2 Cuba Americas 62.2 0.32
3 Gambia Africa 28.4 0.58
4 Mali Africa 33.1 0.38
5 Norway Europe 72.2 0.13
## showing students about reordering
levels(foo$country) # default alphabetical order
[1] "China" "Cuba" "Gambia" "Mali" "Norway"
levels(reorder(foo$country, foo$intercept)) # increasing intercept order
[1] "Gambia" "Mali" "China" "Cuba" "Norway"
## load gdata
#suppressMessages(library(gdata))
library(gdata)
gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.
gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.
Attaching package: 'gdata'
The following object is masked from 'package:stats':
nobs
The following object is masked from 'package:utils':
object.size
## verifying gdata is loaded
search()
[1] ".GlobalEnv" "package:gdata" "package:knitr"
[4] "package:stats" "package:graphics" "package:grDevices"
[7] "package:utils" "package:datasets" "package:methods"
[10] "Autoloads" "package:base"
levels(foo$country) # default alphabetical order
[1] "China" "Cuba" "Gambia" "Mali" "Norway"
## now reorder.factor() from gdata gets called instead of reorder() from base
## I understand why there is no reordering, because FUN has no default in gdata's reorder.factor
## BUT why do I get these Warnings? only when processed with knitr and not in the Console?
levels(reorder(foo$country, foo$intercept)) ## does nothing AND Warnings
Warning: NAs introduced by coercion Warning: NAs introduced by coercion
Warning: NAs introduced by coercion Warning: NAs introduced by coercion
Warning: NAs introduced by coercion Warning: NAs introduced by coercion
Warning: NAs introduced by coercion Warning: NAs introduced by coercion
Warning: NAs introduced by coercion Warning: NAs introduced by coercion
[1] "China" "Cuba" "Gambia" "Mali" "Norway"
## Warnings persist even if I specify FUN, but I do succeed in reordering!
levels(reorder(foo$country, foo$intercept, mean)) ## works BUT Warnings
Warning: NAs introduced by coercion Warning: NAs introduced by coercion
Warning: NAs introduced by coercion Warning: NAs introduced by coercion
Warning: NAs introduced by coercion Warning: NAs introduced by coercion
Warning: NAs introduced by coercion Warning: NAs introduced by coercion
Warning: NAs introduced by coercion Warning: NAs introduced by coercion
[1] "Gambia" "Mali" "China" "Cuba" "Norway"
## force the use of reorder from stats
levels(stats:::reorder.default(foo$country, foo$intercept))
[1] "Gambia" "Mali" "China" "Cuba" "Norway"
sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] gdata_2.13.2 knitr_1.4.1
loaded via a namespace (and not attached):
[1] digest_0.6.3 evaluate_0.4.7 formatR_0.9 grid_3.0.1
[5] gtools_3.0.0 lattice_0.20-15 stringr_0.6.2 tools_3.0.1