From h.wickham at gmail.com  Mon Nov 17 18:34:02 2008
From: h.wickham at gmail.com (hadley wickham)
Date: Mon, 17 Nov 2008 11:34:02 -0600
Subject: [Rd] stringsAsFactors = FALSE
In-Reply-To: <77EB52C6DD32BA4D87471DCD70C8D70028E443@NA-PA-VBE03.na.tibco.com>
References: <f8e6ff050811170510i576d0014qdb22f08c57690bb2@mail.gmail.com>
	<77EB52C6DD32BA4D87471DCD70C8D70028E443@NA-PA-VBE03.na.tibco.com>
Message-ID: <f8e6ff050811170934k3b1c54d8s8eafcf6218e3d70@mail.gmail.com>

On Mon, Nov 17, 2008 at 11:06 AM, William Dunlap <wdunlap at tibco.com> wrote:
>> 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).

Ah, that makes sense.  (Although the conversion to factors just seems
to be a convenient way to achieve the desired effect in this case -
there's no reason they have to be factors in the output)

Hadley

-- 
http://had.co.nz/



[Rd] stringsAsFactors = FALSE
hadley wickham