[Rd] forwarded: bug (?) in cut.POSIXt with "breaks"=integer

Gabor Grothendieck ggrothendieck at gmail.com
Sat Oct 17 16:35:08 CEST 2009


That is usually done with trunc rather than cut since in the case of a
time series we normally don't want a factor result (which is what cut
would give):

trunc(tt, "secs")
trunc(tt, "mins")
# etc

trunc.POSIXt does not support the "30 secs" syntax but trunc.times in
the chron package supports similar functionality:

library(chron)
trunc(as.chron(format(tt)), "00:00:30")

# or relative to GMT
trunc(as.chron(tt)), "00:00:30")

On Thu, Oct 15, 2009 at 1:10 PM, Ben Bolker <bolker at ufl.edu> wrote:
>
> From: Vitalie S. <vitosmail <at> rambler.ru>
> Subject: Bug in cut.POSIXt
> Newsgroups: gmane.comp.lang.r.general
> Date: 2009-10-15 15:47:48 GMT (1 hour and 29 minutes ago)
>
> Hello Everyone,
>
> Before reporting decided to post here first:
>
> tt <- structure(c(1254238817, 1254238859, 1254238969, 1254239080), class =
> c("POSIXt",
>                                                              "POSIXct"),
> tzone = "")
>
> cut.POSIXt(tt, 2)
> #Error in `levels<-.factor`(`*tmp*`, value = character(0)) :
> #  number of levels differs
>
> cut.POSIXt(tt, 2, labels=c("a1", "a2"))
> #[1] a1 a1 a2 a2
> #Levels: a1 a2
>
> cut(tt, "2 mins")
> #[1] 2009-09-29 17:40:00 2009-09-29 17:40:00 2009-09-29 17:42:00
> 2009-09-29 17:44:00
> #Levels: 2009-09-29 17:40:00 2009-09-29 17:42:00 2009-09-29 17:44:00
>
>> sessionInfo()
> R version 2.9.2 Patched (2009-09-24 r50069)
> i386-pc-mingw32
>
> locale:
> LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
> States.1252;LC_MONETARY=English_United
> States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats     graphics  utils     datasets  grDevices methods   base
>>
>
> Vitalie.
>
>
> ======================
>
>  I would agree that this is a bug ... this patch gets rid of the error,
> but leaves the labels very ugly -- given enough hours I might be able to
> figure out how to make the labels nice, but someone else can probably do
> it quicker (see comments in code below)
>
>
>  It would probably also be worth adding something like
>
> cut(Dates,2)
>
>  around line 5067 of reg-tests-1.R
>
>
> --- datetime.R  2009-10-15 13:01:15.000000000 -0400
> ***************
> *** 764,770 ****
>        }
>      } 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)])
>      res
>  }
>
> --- 764,782 ----
>        }
>      } else stop("invalid specification of 'breaks'")
>      res <- cut(unclass(x), unclass(breaks), labels = labels, right =
> right, ...)
> !     if(is.null(labels)) {
> !       if (is.numeric(breaks) && length(breaks) == 1L)
> !         levels(res) <- as.character(breaks[-length(breaks)])
> !       } else {
> !         ## ?? what should happen here?
> !         ## this version simply leaves the break values
> !         ## as numeric (and very ugly)
> !         ## I don't know how to safely convert the breakpoints
> !         ## back to a character format (e.g.
> !         ## levels(res) = [1970-01-01 00:00:00,1970-01-01 12:00:00)
> !         ## instead of [18000,61200)
> !       }
> !     }
>      res
>  }
>
>
> --
> Ben Bolker
> Associate professor, Biology Dep't, Univ. of Florida
> bolker at ufl.edu / www.zoology.ufl.edu/bolker
> GPG key: www.zoology.ufl.edu/bolker/benbolker-publickey.asc
>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



More information about the R-devel mailing list