From felix at nfrac.org  Thu Apr 29 02:04:33 2010
From: felix at nfrac.org (Felix Andrews)
Date: Thu, 29 Apr 2010 10:04:33 +1000
Subject: [Rd] bug in cut.POSIXt(..., breaks = <numeric>) and cut.Date
Message-ID: <q2k94730b8a1004281704kfc24652q15a56e4571a28e37@mail.gmail.com>

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/



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