[Rd] bug in cut.POSIXt(..., breaks = <numeric>) and cut.Date

Felix Andrews felix at nfrac.org
Thu Apr 29 02:04:33 CEST 2010


x <- seq(as.POSIXct("2000-01-01"), by = "days", length = 20)

cut(x, breaks = 3)
# Error in `levels<-.factor`(`*tmp*`, value = character(0)) :
#   number of levels differs

cut(as.Date(x), breaks = 3)
# Error in `levels<-.factor`(`*tmp*`, value = character(0)) :
#   number of levels differs



Index: base/R/datetime.R
===================================================================
--- base/R/datetime.R   (revision 51857)
+++ base/R/datetime.R   (working copy)
@@ -775,7 +775,11 @@
         }
     } else stop("invalid specification of 'breaks'")
     res <- cut(unclass(x), unclass(breaks), labels = labels, right =
right, ...)
-    if(is.null(labels)) levels(res) <- as.character(breaks[-length(breaks)])
+    if(is.null(labels)) {
+        if (is.numeric(breaks))
+            levels(res) <- as.character(x[!duplicated(res)])
+        else levels(res) <- as.character(breaks[-length(breaks)])
+    }
     res
 }

Index: base/R/dates.R
===================================================================
--- base/R/dates.R      (revision 51857)
+++ base/R/dates.R      (working copy)
@@ -362,7 +362,11 @@
     } else stop("invalid specification of 'breaks'")
     res <- cut(unclass(x), unclass(breaks), labels = labels,
                right = right, ...)
-    if(is.null(labels)) levels(res) <- as.character(breaks[-length(breaks)])
+    if(is.null(labels)) {
+        if (is.numeric(breaks))
+            levels(res) <- as.character(x[!duplicated(res)])
+        else levels(res) <- as.character(breaks[-length(breaks)])
+    }
     res
 }



Regards
-Felix

-- 
Felix Andrews / 安福立
Postdoctoral Fellow
Integrated Catchment Assessment and Management (iCAM) Centre
Fenner School of Environment and Society [Bldg 48a]
The Australian National University
Canberra ACT 0200 Australia
M: +61 410 400 963
T: + 61 2 6125 4670
E: felix.andrews at anu.edu.au
CRICOS Provider No. 00120C
-- 
http://www.neurofractal.org/felix/



More information about the R-devel mailing list