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

Peter.Marx at knorr-bremse.com Peter.Marx at knorr-bremse.com
Fri Aug 25 17:37:31 CEST 2006


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.

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"



More information about the R-help mailing list