RE: [R] Surprise when indexing with a factor.

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

From: Liaw, Andy (andy_liaw@merck.com)
Date: Sat 08 May 2004 - 22:23:03 EST


Message-id: <3A822319EB35174CA3714066D590DCD504AF7D50@usrymx25.merck.com>

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}}

______________________________________________
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