[R] factor(300000, levels=1:300000) gives NA

Suharto Anggono Suharto Anggono suharto_anggono at yahoo.com
Sat Sep 20 12:52:15 CEST 2014


In R:

> factor(300000, levels=1:300000)
[1] <NA>
300000 Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ... 300000

The NA above is undesirable in my view, because 300000 is in 1:300000.


I have just got bitten by it.


I have figured out why it happens. The results of 'as.character' are different.

> as.character(300000)
[1] "3e+05"
> as.character((1:300000)[300000])
[1] "300000"


> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base



More information about the R-help mailing list