[R] Problem with modification of [.factor

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Fri Jul 19 14:00:00 CEST 2002


On Fri, 19 Jul 2002, Frank E Harrell Jr wrote:

> Under
>
> platform i586-pc-linux-gnu
> arch     i586
> os       linux-gnu
> system   i586, linux-gnu
> status
> major    1
> minor    5.0    [1.5.1 not available for Mandrake 8.1]
> year     2002
> month    04
> day      29
> language R
>
> when I modify [.factor to change the default drop=FALSE to drop=TRUE as such:
>
> '[.factor' <- function(x, i, drop=TRUE) {
>     y <- NextMethod("[")
>     class(y) <- class(x)
>     attr(y, "contrasts") <- attr(x, "contrasts")
>     attr(y, "levels") <- attr(x, "levels")
>     if (drop)
>         factor(y)
>     else y
> }
>
> I get a recursion error:
>
> > x <- factor(1:2,1:2,c('a','b'))
> > x[]
>
> Error in inherits(x, "factor") : evaluation is nested too deeply: infinite recursion?
>
> If anyone has an explanation/fix I would appreciate getting it.

traceback() is your friend:

5: "[.factor"(levels, is.na(match(levels, exclude)))
4: levels[is.na(match(levels, exclude))]
3: factor(y)
2: "[.factor"(x, )
1: x[]

I would caution you against changing the behaviour of a system class, not
least because with namespaces it will work differently and probably not
achieve what you wanted.  Define your own class instead.

But if you really, really want to do this, you need (in R < 1.6.0) to
redefine factor() too.

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list