From h.wickham at gmail.com  Mon Nov 17 18:32:36 2008
From: h.wickham at gmail.com (hadley wickham)
Date: Mon, 17 Nov 2008 11:32:36 -0600
Subject: [Rd] stringsAsFactors = FALSE
In-Reply-To: <alpine.LFD.2.00.0811171443450.8612@gannet.stats.ox.ac.uk>
References: <f8e6ff050811170510i576d0014qdb22f08c57690bb2@mail.gmail.com>
	<alpine.LFD.2.00.0811171443450.8612@gannet.stats.ox.ac.uk>
Message-ID: <f8e6ff050811170932s51bc0219v9cde08e8f812c555@mail.gmail.com>

On Mon, Nov 17, 2008 at 9:03 AM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
> On Mon, 17 Nov 2008, hadley wickham wrote:
>
>> Hi all,
>>
>> I love the option to not automatically convert strings into factors,
>> but there are three places that the current option doesn't work where
>> I think it should:
>
> Perhaps you mean 'when I would like it to'?   Things *should* work as
> documented, surely?

In an ideal world, I think things should be documented *and* consistent.

>> options(stringsAsFactors = FALSE)
>>
>> str(expand.grid(letters))
>> str(type.convert(letters))
>>
>> df <- read.fwf(textConnection(paste(letters,collapse="\n")), 1)
>> str(df)
>
> I get
>
>> str(df)
>
> 'data.frame':   26 obs. of  1 variable:
>  $ V1: chr  "a" "b" "c" "d" ...
>
> so what is wrong with that?  read.fwf just calls read.table, so the default
> options of read.table apply.

Ok, that's weird. I get factors.

>> I think type.convert and read.fwf can be fixed by giving them a
>> stringsAsFactors argument and then using asis = !stringsAsFactors
>> (like read.table).
>
> Seems to me that there is nothing wrong with read.fwf.  For type.convert()
> we could have the default
>
> as.is = !default.stringsAsFactors()
>
> but I think a strong case needs to be made to change the documented
> behaviour.

Well, my intuition was that type.convert should mirror the behaviour
of read.table, since it is what does the conversion behind the scenes.
 I can of course change my own code.

>>  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.
>
> Nor I am, but it goes back to at least r2107, over 10 years ago.  I don't
> see much problem with adding a 'stringsAsFactors' argument there.

Great, thanks.

Hadley

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



[Rd] stringsAsFactors = FALSE
hadley wickham