Dear all,

pmin/pmax used to work with ordered factors but fail now since this summer when those functions have tried to handle more cases.

A simple way to trigger the issue is:

> min(ordered(c(1,5,6)))
[1] 1
Levels: 1 < 5 < 6
> pmin(ordered(c(1,5,6)), ordered(c(1,5,6)))
Error in `mostattributes<-`(`*tmp*`, value = attributes(elts[[1L]])) :
  adding class "factor" to an invalid object

A simple fix is to explicitly test for the ordered class and use the internal method in that case as proposed in the attached patch.

    Yours,

        Erwan

Index: src/library/base/R/pmax.R
===================================================================
--- src/library/base/R/pmax.R	(revision 71694)
+++ src/library/base/R/pmax.R	(working copy)
@@ -24,7 +24,7 @@
     elts <- list(...)
     if(length(elts) == 0L) stop("no arguments")
     i4 <- isS4(elts[[1L]])
-    if(!i4 && all(vapply(elts, function(x) is.atomic(x) && !is.object(x), NA))) {
+    if(!i4 && all(vapply(elts, function(x) is.atomic(x) && (!is.object(x) || is.ordered(x)), NA))) {
 	## NB: NULL passes is.atomic
 	mmm <- .Internal(pmax(na.rm, ...))
     } else {
@@ -68,7 +68,7 @@
     elts <- list(...)
     if(length(elts) == 0L) stop("no arguments")
     i4 <- isS4(elts[[1L]])
-    if(!i4 && all(vapply(elts, function(x) is.atomic(x) && !is.object(x), NA))) {
+    if(!i4 && all(vapply(elts, function(x) is.atomic(x) && (!is.object(x) || is.ordered(x)), NA))) {
 	mmm <- .Internal(pmin(na.rm, ...))
     } else {
 	mmm <- as.vector(elts[[1L]]) # attr(mmm, "dim") <- NULL  # dim<- would drop names
______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to