From ripley at stats.ox.ac.uk  Wed Nov 15 10:31:18 2006
From: ripley at stats.ox.ac.uk (Brian Ripley)
Date: Wed, 15 Nov 2006 09:31:18 +0000 (GMT)
Subject: [Rd] c.factor
In-Reply-To: <Pine.GSO.4.56.0611141218410.17778@durian.statsci.com>
References: <946928E281E8FD4B91D5B2CC3C91967D6AA9A0@lnex01.CONCORDIAFUNDS.COM>
	<1163526685.4492.20.camel@localhost.localdomain>
	<1163527030.4492.23.camel@localhost.localdomain>
	<Pine.LNX.4.64.0611141809050.25751@gannet.stats.ox.ac.uk>
	<Pine.GSO.4.56.0611141218410.17778@durian.statsci.com>
Message-ID: <Pine.LNX.4.64.0611150926070.19618@auk.stats>

On Tue, 14 Nov 2006, Bill Dunlap wrote:

> On Tue, 14 Nov 2006, Prof Brian Ripley wrote:
>
>> Well, R has managed without a factor method for c() for most of its decade
>> of existence (not that it originally had factors as we know them).
>>
>> I would argue that factors are best viewed as an enumeration type, and
>> anything which silently changes their level set is a bad idea.  I can see
>> a case for a c() method for factors that combines factors with the same
>> level sets, but I can also see this is best done by users who know the
>> level sets are same (c.factor would have to expend a considerable effort
>> to check).
>>
>> You also need to consider the dispatch rules.  c.factor will be called
>> whenever the first argument is a factor, whatever the others are. S4 (I
>> think, definitely S4-based versions of S-PLUS) has an alternative concat()
>> that works differently (recursively) and seems a more natural model.
>
> In addition, c() has always had a double meaning of
>  (a) turning an object into a simple "vector" (an object
>      without "attributes"), as in
>      > c(factor(c("Cat","Dog","Cat")))
>      [1] 1 2 1
>      > c(data.frame(x=1:2,y=c("Dog","Cat")))
>      $x
>      [1] 1 2
>
>      $y
>      [1] Dog Cat
>      Levels: Cat Dog

To my surprise that was not documented at all on the R help page, and I've 
clarified it.  (BTW, at least in R it does not remove names, just all 
other attributes.)

>  (b) concatenating several such vectors into one.
>
> The proposed c.factor does only (b).

(Strictly not, as a factor is not a vector.)

But the help page explicitly only describes the default method, and some 
of the other methods do preserve some attributes, AFAIR.

> Should we just
> throw c() into the ash heap and use as.vector() or
> concat() instead?
>
> The whole concept of concatenating objects of disparate
> types is suspect.

I think working on a concat() for R would be helpful.  I vaguely recalled 
something like it in the Green Book, but the index does not help (but then 
it is not very complete).

Brian

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] c.factor
Brian Ripley