From wdunlap at tibco.com  Mon Nov 17 18:06:49 2008
From: wdunlap at tibco.com (William Dunlap)
Date: Mon, 17 Nov 2008 09:06:49 -0800
Subject: [Rd] stringsAsFactors = FALSE
In-Reply-To: <f8e6ff050811170510i576d0014qdb22f08c57690bb2@mail.gmail.com>
References: <f8e6ff050811170510i576d0014qdb22f08c57690bb2@mail.gmail.com>
Message-ID: <77EB52C6DD32BA4D87471DCD70C8D70028E443@NA-PA-VBE03.na.tibco.com>

> 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 



[Rd] stringsAsFactors = FALSE
William Dunlap