Skip to content

inconsistency in ?factor

3 messages · Petr Savicky, Berwin A Turlach

#
In the almost current development version (2009-05-22 r48594) and also in 
  https://svn.r-project.org/R/trunk/src/library/base/man/factor.Rd
?factor contains (compare the formulations marked by ^^^^^^)

\section{Warning}{
  The interpretation of a factor depends on both the codes and the
  \code{"levels"} attribute.  Be careful only to compare factors with
  the same set of levels (in the same order).  
                          ^^^^^^^^^^^^^^^^^

\section{Comparison operators and group generic methods}{
  ...

  Only \code{==} and \code{!=} can be used for factors: a factor can
  only be compared to another factor with an identical set of levels
  (not necessarily in the same ordering) or to a character vector.
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In the development version 2009-05-22 r48594, the latter formulation
"not necessarily in the same ordering" is correct.

  f1 <- factor(c("a", "b", "c", "c", "b", "a", "a"), levels=c("a", "b", "c"))
  f2 <- factor(c("a", "b", "c", "c", "b", "a", "c"), levels=c("c", "b", "a"))
  f1 == f2 # [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE

The first formulation "Be careful to compare ... levels in the same order" may
be just a warning against a potential problem if the levels have different order,
however this is not clear.

Petr.
#
G'day Petr,

On Mon, 25 May 2009 09:04:14 +0200
Petr Savicky <savicky at cs.cas.cz> wrote:

            
Well, the first statement is a remark on comparison in general while
the second statement is specific to "comparison operators and generic
methods".  There are other ways of comparing objects; note:

R> f1 <- factor(c("a", "b", "c", "c", "b", "a"), levels=c("a", "b", "c"))
R> f2 <- factor(c("a", "b", "c", "c", "b", "a"), levels=c("c", "b", "a"))
R> f1==f2
[1] TRUE TRUE TRUE TRUE TRUE TRUE
R> identical(f1,f2)
[1] FALSE
R> all.equal(f1,f2)
[1] "Attributes: < Component 2: 2 string mismatches >"

Just my 2c.

Cheers,

	Berwin

=========================== Full address =============================
Berwin A Turlach                            Tel.: +65 6516 4416 (secr)
Dept of Statistics and Applied Probability        +65 6516 6650 (self)
Faculty of Science                          FAX : +65 6872 3919       
National University of Singapore     
6 Science Drive 2, Blk S16, Level 7          e-mail: statba at nus.edu.sg
Singapore 117546                    http://www.stat.nus.edu.sg/~statba
#
On Mon, May 25, 2009 at 03:58:06PM +0800, Berwin A Turlach wrote:
I see. We have to distinguish comparison of the objects and their components.

Let me propose the following formulation
  Two factors may be identical only if they have the same set of
  levels (in the same order).
instead of
  Be careful only to compare factors with the same set of
  levels (in the same order).

Petr.