[R] Surprise when indexing with a factor.

Liaw, Andy andy_liaw at merck.com
Sat May 8 14:23:03 CEST 2004


It may be educational to read ?factor before you use a factor for some
operation (such as subscripting), I guess.  In part, it says:

Value:

     'factor' returns an object of class '"factor"' which has a set of
     numeric codes the length of 'x' with a '"levels"' attribute of
     mode 'character'.  If 'ordered' is true (or 'ordered' is used) the
     result has class 'c("ordered", "factor")'.

In other words, a factor is a numeric vector with a "levels" attribute.
What do you expect to happen when you use a numeric vector as subscript?

Andy

> From: pallier
> 
> Hello,
> 
> I had been thinking for years, without having ever checked (shame on 
> me), that indexing a named vector by a factor 'f' produced the same 
> results as indexing it by 'as.character(f)'. I was wrong, as the 
> following example shows:
> 
>   (m <- c(a=1,b=2))
>   (f <- factor(c(1,2),labels=c('b','a')))
> 
>   m[f]
>   m[as.character(f)]
>   m[as.numeric(f)]
> 
> When the labels of a factor are not sorted alphabetically, m[f] and 
> m[as.character(f)] yield different results. m[f] is actually 
> equivalent 
> to m[as.numeric(f)].
> 
> I'm not the first to have been beaten by this (documented) 
> feature, as I 
> discovered in a recent thread ("[R] Indexing by factor misfeature", 
> <http://tolstoy.newcastle.edu.au/R/help/04/04/0937.html> see 
> http://tolstoy.newcastle.edu.au/R/help/04/04/0938.html).
> 
> If I am breaking the posting rules by writing again about a previous 
> subject, it is only
> to suggest to add a short notice about that behavior in the 
> section of 
> 'R-intro.pdf' about 'index vectors'. Currently, it does not 
> mention the 
> use of factors as indexes (It is true that factors are only defined 
> later).  At least, this manual could tell the reader to read the help 
> about Extract, or type '?"["' (which did not occur to me 
> before writing 
> this message...).
> 
> Christophe Pallier
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}




More information about the R-help mailing list