Steve Lianoglou
2009-May-18 22:20 UTC
[R] stringsAsFactors param in expand.grid not working
Hi all, I've (tried) to look through the bug tracker, and gmane-search the R list to see if this has been mentioned before, and it looks like it hasn't. According to the R 2.9.0 release notes[1], the expand.grid function should now take a stringsAsFactor=LOGICAL argument which controls whether or not the function coerces strings as factors. While the parameter is indeed in the function, a quick examination of the function's source shows that the value of this argument is never checked, and all strings are converted to factors as a matter of course. The fix is pretty easy, and I believe only requires changing the `if` check here: if (!is.factor(x) && is.character(x)) x <- factor(x, levels = unique(x)) To: if (!is.factor(x) && is.character(x) && stringsAsFactors) x <- factor(x, levels = unique(x)) I can open a ticket regarding this issue and add this there if necessary. Thanks, -steve [1] http://article.gmane.org/gmane.comp.lang.r.general/146891
On 19/05/2009, at 10:20 AM, Steve Lianoglou wrote:> Hi all, > > I've (tried) to look through the bug tracker, and gmane-search the > R list to > see if this has been mentioned before, and it looks like it hasn't. > > According to the R 2.9.0 release notes[1], the expand.grid function > should now > take a stringsAsFactor=LOGICAL argument which controls whether or > not the > function coerces strings as factors. While the parameter is indeed > in the > function, a quick examination of the function's source shows that > the value > of this argument is never checked, and all strings are converted to > factors > as a matter of course. > > The fix is pretty easy, and I believe only requires changing the > `if` check > here: > > if (!is.factor(x) && is.character(x)) > x <- factor(x, levels = unique(x)) > > To: > > if (!is.factor(x) && is.character(x) && stringsAsFactors) > x <- factor(x, levels = unique(x)) > > I can open a ticket regarding this issue and add this there if > necessary. > > Thanks, > -steve > > [1] http://article.gmane.org/gmane.comp.lang.r.general/146891While we're at it --- would it not make sense to have the stringsAsFactors argument (once it's working) of expand.grid() default to options() $stringsAsFactors, rather than to FALSE? This would make no difference to me personally, since I set options(stringsAsFactors=FALSE) in my .Rprofile. But it might make some people happier .... cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
>>>>> "RT" == Rolf Turner <r.turner at auckland.ac.nz> >>>>> on Tue, 19 May 2009 11:02:08 +1200 writes:RT> On 19/05/2009, at 10:20 AM, Steve Lianoglou wrote: >> Hi all, >> >> I've (tried) to look through the bug tracker, and gmane-search the >> R list to >> see if this has been mentioned before, and it looks like it hasn't. >> >> According to the R 2.9.0 release notes[1], the expand.grid function >> should now >> take a stringsAsFactor=LOGICAL argument which controls whether or >> not the >> function coerces strings as factors. While the parameter is indeed >> in the >> function, a quick examination of the function's source shows that >> the value >> of this argument is never checked, and all strings are converted to >> factors >> as a matter of course. >> >> The fix is pretty easy, and I believe only requires changing the >> `if` check >> here: >> >> if (!is.factor(x) && is.character(x)) >> x <- factor(x, levels = unique(x)) >> >> To: >> >> if (!is.factor(x) && is.character(x) && stringsAsFactors) >> x <- factor(x, levels = unique(x)) >> >> I can open a ticket regarding this issue and add this there if >> necessary. Thank you, but it's not necessary anymore; we (R Core Team) have already opened a ticket... >> Thanks, >> -steve >> >> [1] http://article.gmane.org/gmane.comp.lang.r.general/146891 RT> While we're at it --- would it not make sense to have the RT> stringsAsFactors RT> argument (once it's working) of expand.grid() default to options() RT> $stringsAsFactors, RT> rather than to FALSE? NNNNNNNNNNNNNNNOOOOOOOOOOOOOOOOOOOOOOO !!!!! RT> This would make no difference to me personally, since I set RT> options(stringsAsFactors=FALSE) in my .Rprofile. But it might make some RT> people happier .... As I have said several times (on R-devel rather than R-help), I strongly believe that the introduction of such an option has been one of very few very bad choices we (the R core team) have made in the recent past. Setting an option should *never* influence (basic) R computational functionality; options() typically should only influence print()ing, maybe plotting() and similar I/O. I still hope that I'll find the stamina in the not so distant future to convince a majority within R-core to abolish this abominational option, of course with transitional help for the stringsAsFactors_=_FALSE junkies. Martin Maechler