[R] How to get back POSIXct format after calculating with hist() results

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 25 17:53:03 CEST 2006


On Fri, 25 Aug 2006, Peter.Marx at knorr-bremse.com wrote:

> Hi,
> 
> I have a casting/formatting question on hist.POSIXt:
> 
> The histogram plot from POSIXct works perfect (with help of Prof. Ripley
> -thanks!).
> When processing the hist(plot=FALSE) output and then plotting the
> results over the x-axis (bins) coming from hist(), I lose the date/time
> labels, getting instead integers displayed.
> 
> Trying to cast the $breaks with as.POSIXct gives silly results with
> StarTrek-like years.

But you actually called as.POSIXct(as.Date()) on them, which was `silly'.

class(login_bins$mids) <- class(logins$DateTime)

should do the trick.

> What is the proper way to get back the old YY-MM-DD hh:mm:ss labels ?
> 
> Peter  
> 
> 
> 
> > str(logins)
> `data.frame':   25792 obs. of  1 variable:
>  $ DateTime:'POSIXct', format: chr  "2006-08-01 00:00:02" "2006-08-01
> 00:00:25" "2006-08-01 00:00:41" ...
> 
> > login_bins<-hist(samples$DateTime, "hours", freq=TRUE,plot=FALSE)
> 
> > str(login_bins)
> List of 7
>  $ breaks     : num [1:337] 1.15e+09 1.15e+09 1.15e+09 1.15e+09 1.15e+09
> ...
>  $ counts     : int [1:336] 6 10 45 25 40 87 257 356 309 214 ...
>  $ intensities: num [1:336] 0.000233 0.000388 0.001744 0.000969 0.001550
> ...
>  $ density    : num [1:336] 6.46e-08 1.08e-07 4.85e-07 2.69e-07 4.31e-07
> ...
>  $ mids       : num [1:336] 1.15e+09 1.15e+09 1.15e+09 1.15e+09 1.15e+09
> ...
>  $ xname      : chr "logins$DateTime"
>  $ equidist   : logi TRUE
>  - attr(*, "class")= chr "histogram"
> 
> ... Then I do some calculations with the login_bins$counts, resulting in
> a new vector:
> 
> >str(userabs)
>  int [1:336] 1 -3 34 39 68 127 347 641 844 943 ...
> 
> When I now plot this y over the x-axis coming from the previous
> histogram x-axis with
> 
> >plot(login_bins$mids, userabs) 
> 
> I get the x-axis of labelled with the nums 1154400000, 1154800000...
> (number of seconds since 1970 ?)
> 
> Trying to convert this gives funny results.
> 
> >as.POSIXct(as.Date(login_bins$mids[1]))
> [1] "2568-10-12 02:00:00 W. Europe Daylight Time"
> 
> > as.POSIXct(as.Date(login_bins$mids[2]))
> [1] "2578-08-21 02:00:00 W. Europe Daylight Time"
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

-- 
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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list