[R] Surprise when indexing with a factor.

About this list Date view Thread view Subject view Author view Attachment view

From: pallier (pallier@lscp.ehess.fr)
Date: Sat 08 May 2004 - 19:33:46 EST


Message-id: <409CA97A.6030405@lscp.ehess.fr>

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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Mon 31 May 2004 - 23:05:08 EST