[R] read mignight as 24:00 and not as 0:00

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Mon Jul 16 23:18:00 CEST 2012


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Sandy Adriaenssens
> Sent: Monday, July 16, 2012 8:10 AM
> To: r-help at r-project.org
> Subject: Re: [R] read mignight as 24:00 and not as 0:00
> 
> Hi Dan,
> 
> Thank you for your answer. I 'll try to provide some more information:
> 
> I have only hourly data that go from 1:00:00 to 24:00:00, no minutes or
> seconds. These data represent the average of measured valuesof the
> previous
> hour (so 1:00:00 is the average of 0:00:00 to 1:00:00 data).
> I can indeed substract 1 hour from all date/time values to solve the
> problem. However, I would also like to look at hourly variation within
> a day
> and then you always need to take into account the difference of 1 hour,
> which will get confusing.
> 
> So, the easiest would be to convert all 0:00 values to 24:00 and set
> the
> date one day back.
> 
> z.dr <- data.frame(times=c(seq.POSIXt(ISOdate(2011,1,1),
> ISOdate(2011,1,31),
> by ="hour" )))
> 
> This generates a date/time serie looking like mine but shorter. Then I
> tried
> converting all 0:00 to 24:00 values by the following loop:
> 
> for(i in nrow(z.dr))   {
> 	z.dr$times[i] <- ifelse(as.numeric(format(z.dr$times[i],
> "%H"))=="0",
> 		z.dr$times[i]+86400, z.dr$times[i])
> }
> 
> However, here I get the error "Error in as.POSIXct.numeric(value) :
> 'origin'
> must be supplied". Without the loop it works, but the output is a
> numerical
> value.
> This is also not a good solution because the day is not changed by this
> command.
> 
> Or is this just not possible to change?
> 
> Regards,
> Sandy

Sandy,

It is not possible to do you want in the way that you want, i.e. using R date/time values, because midnight is the beginning of a new day and not the end of a day.  The time 24:00 does not exist in all the time keeping systems that I am aware of.  If you want use R date/time objects, then I would read the data, subtract 1 hour from the value read, and reconceptualize your analyses as time representing when the hour of interest began, rather than when it ended.  You can always keep the text representation of the date/time if you wish to present your analyses in the 1:00 to 24:00 format.  Or as someone else suggested, you extract the hour as a separate object represented as 1 to 24.

Sorry I can't be of much more help,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204




More information about the R-help mailing list