[R] stringsAsFactors param in expand.grid not working

Steve Lianoglou mailinglist.honeypot at gmail.com
Tue May 19 00:20:44 CEST 2009

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 

if (!is.factor(x) && is.character(x)) 
    x <- factor(x, levels = unique(x))


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.


[1] http://article.gmane.org/gmane.comp.lang.r.general/146891

More information about the R-help mailing list