From ripley at stats.ox.ac.uk  Tue Apr 20 13:51:46 2004
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue Apr 20 13:51:49 2004
Subject: [Rd] Re: [R] Unexpected behaviour of identical (PR#6799)
In-Reply-To: <20040420114244.86AF4104BA@slim.kubism.ku.dk>
Message-ID: <Pine.LNX.4.44.0404201249140.11664-100000@gannet.stats>

On Tue, 20 Apr 2004 p.dalgaard@biostat.ku.dk wrote:

> "Swinton, Jonathan" <Jonathan.Swinton@astrazeneca.com> writes:
> 
> >  # works as expected
> > > ac <- c('A','B');
> > > identical(ac,ac[1:2])
> > [1] TRUE
> >  
> >  #but
> > > af <- factor(ac)
> > > identical(af,af[1:2])
> > [1] FALSE
> > 
> > Any opinions?
> 
> Did a cross-check with Splus and it doesn't do that , so I think it
> qualifies as a bug. Shouldn't be too hard to fix (might lose a little
> efficiencty though).

No, it comes from

> get("[.factor")
function (x, i, drop = FALSE)
{
    y <- NextMethod("[")
    class(y) <- oldClass(x)
    attr(y, "contrasts") <- attr(x, "contrasts")
    attr(y, "levels") <- attr(x, "levels")
    if (drop)
        factor(y)
    else y
}

> attributes(af[1:2, drop=TRUE])
$levels
[1] "A" "B"

$class
[1] "factor"

> attributes(af[1:2, drop=FALSE])
$class
[1] "factor"

$levels
[1] "A" "B"

and one needs to swap the orders.  I am about to commit the change.


-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


[Rd] Re: [R] Unexpected behaviour of identical (PR#6799)
Prof Brian Ripley