[R] Surprise when indexing with a factor.

pallier pallier at lscp.ehess.fr
Sat May 8 11:33:46 CEST 2004


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




More information about the R-help mailing list