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. Thanks in advance, -Frank -- Frank E Harrell Jr Prof. of Biostatistics & Statistics Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences U. Virginia School of Medicine http://hesweb1.med.virginia.edu/biostat -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 > }A simple fix is 'x <- unclass(x)' prior to NextMethod(). But before doing this, I'd consider Prof. Ripley's comments on this topic extremely carefully.> > 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. > > Thanks in advance, -Frank > -- > Frank E Harrell Jr Prof. of Biostatistics & Statistics > Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences > U. Virginia School of Medicine http://hesweb1.med.virginia.edu/biostat > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- David A. James Statistics Research, Room 2C-253 Phone: (908) 582-3082 Bell Labs, Lucent Technologies Fax: (908) 582-3340 Murray Hill, NJ 09794-0636 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._