[Rd] c.factor

Brian Ripley ripley at stats.ox.ac.uk
Wed Nov 15 10:31:18 CET 2006


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



More information about the R-devel mailing list