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

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sat Sep 20 17:51:25 CEST 2014


I would say having 300000 levels is a bad idea... You should be re-thinking your analysis.

If you are still convinced that this is necessary, then do it right:

factor(300000L, levels=1:300000)

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On September 20, 2014 3:52:15 AM PDT, Suharto Anggono Suharto Anggono <suharto_anggono at yahoo.com> wrote:
>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
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list