[Rd] stringsAsFactors = FALSE

William Dunlap wdunlap at tibco.com
Mon Nov 17 18:06:49 CET 2008


> From: r-devel-bounces at r-project.org 
> [mailto:r-devel-bounces at r-project.org] On Behalf Of hadley wickham
> Sent: Monday, November 17, 2008 5:10 AM
> To: r-devel at r-project.org
> Subject: [Rd] stringsAsFactors = FALSE
> ...
> The key lines in 
> expand.grid would seem to be
> 
>             if (!is.factor(x) && is.character(x))
>                 x <- factor(x, levels = unique(x))
> 
> but I'm not sure why they are being converted to factors in 
> the first place.

I think expand.grid converts input strings to factors so they
retain the order they have in the input.  (Note that the levels
argument is unique(x), not the sort(unique(x)) that data.frame uses.)
People generally give expand.grid sorted input and expect it to
not alter the order (the order of the levels affects tables and
and some plots).

>
lapply(expand.grid(Grade=c("Bad","Good","Better"),Size=c("Small","Medium
","Large")), levels)
$Grade
[1] "Bad"    "Good"   "Better"

$Size
[1] "Small"  "Medium" "Large"

>
lapply(data.frame(Grade=c("Bad","Good","Better"),Size=c("Small","Medium"
,"Large")), levels)
$Grade
[1] "Bad"    "Better" "Good"

$Size
[1] "Large"  "Medium" "Small"


I have nothing against adding the stringsAsFactors argument to
expand.grid.

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com 



More information about the R-devel mailing list