From ripley at stats.ox.ac.uk  Mon Aug 30 09:28:24 2010
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon, 30 Aug 2010 08:28:24 +0100 (BST)
Subject: [Rd] No [[<-.factor()
In-Reply-To: <19574.9989.644763.347483@lynne.math.ethz.ch>
References: <77EB52C6DD32BA4D87471DCD70C8D700035B3C70@NA-PA-VBE03.na.tibco.com>
	<19574.9989.644763.347483@lynne.math.ethz.ch>
Message-ID: <alpine.LFD.2.00.1008300814580.18335@gannet.stats.ox.ac.uk>

On Thu, 26 Aug 2010, Martin Maechler wrote:

>>>>>> "WD" == William Dunlap <wdunlap at tibco.com>
>>>>>>     on Wed, 25 Aug 2010 17:31:27 -0700 writes:
>
>    WD> Should there be a [[<-.factor() that either throws
>    WD> an error or acts like [<-.factor() to avoid making
>    WD> an illegal object of class factor?
>
> Yes, one or the other.
> Note that both `[<-` and `[[<-` are "Primitive" and do some
> "informal dispatch" in addition to the formal method dispatch,

I don't think so.  do_subassign2 does formal method dispatch, albeit 
internally, but nothing else (do_assign2_dflt selects only by 
typeof()).

> so there's no need for an explicit '[[<-.factor' method definition.
> For factors, it seems rational and "expectable" that
> double bracket behaves as single bracket when *one* element is
> assigned.
>   z[[2:3]] <- c("Two, Three")
> however  should continue to give an error.

I added a [[<-.factor yesterday.  Note that we do have a [<-.factor 
which works on the integer codes, and I simply copied its logic.

BTW, you probably meant

z[[2:3]] <- c("Two", "Three")


>
> Martin Maechler, ETH Zurich
>
>    >> z <- factor(c("Two","Two","Three"), levels=c("One","Two","Three"))
>    >> z
>    WD> [1] Two   Two   Three
>    WD> Levels: One Two Three
>    >> str(z)
>    WD> Factor w/ 3 levels "One","Two","Three": 2 2 3
>    >> z[[2]] <- "One"
>    >> str(z) # the .Data part is now character
>    WD> Factor w/ 3 levels "One","Two","Three": 2 One 3
>    >> z
>    WD> [1] <NA> <NA> <NA>
>    WD> Levels: One Two Three
>    >> z[2] <- "One"
>    WD> Error in class(x) <- cx : adding class "factor" to an invalid object
>
>    WD> Bill Dunlap
>    WD> Spotfire, TIBCO Software
>    WD> wdunlap tibco.com
>
>    WD> ______________________________________________
>    WD> R-devel at r-project.org mailing list
>    WD> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at 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] No [[<-.factor()
Prof Brian Ripley