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

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Mon Jul 16 01:33:51 CEST 2012


Extract the date separately from the time initially, and keep it separate. When you want to process daily data, use that column.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Daniel Nordlund <djnordlund at frontier.com> wrote:

>> -----Original Message-----
>> From: r-help-bounces at r-project.org
>[mailto:r-help-bounces at r-project.org]
>> On Behalf Of Sandy Adriaenssens
>> Sent: Friday, July 13, 2012 3:52 AM
>> To: r-help at r-project.org
>> Subject: [R] read mignight as 24:00 and not as 0:00
>> 
>> Dear all,
>> 
>> I have dataset which contains date and time in the format
>> "yearmonthdayhour". I can read in these data correctly as follows:
>> 
>> mydata <- read.csv("pm10_corine_gridcel_hourly_2011.csv", header =
>TRUE)
>> mydata$date <- as.POSIXct(strptime(mydata$date, format = "%Y%m%d%H",
>> tz="UTC"))
>> 
>> However, midnight is defined as 24:00 in my original file (so the end
>of
>> the
>> day), while the POSIXct function changes this to 0:00 (the beginning
>of
>> the
>> next day).
>> So, my data now go from January 1 2011 1:00 to Januari 1 2012 0:00,
>in
>> stead
>> of December 31 2011 24:00.
>> 
>> summary(mydata$date)
>>                  Min.               1st Qu.                Median
>> "2011-01-01 01:00:00" "2011-04-02 06:45:00" "2011-07-02 12:30:00"
>>                  Mean               3rd Qu.                  Max.
>> "2011-07-02 12:30:00" "2011-10-01 18:15:00" "2012-01-01 00:00:00"
>> 
>> I would like to change this 0:00 to 24:00 again since I want to
>include
>> these values in daily averages of the previous day (and not of the
>next
>> day). So the day of the month should also be diminished by 1.
>> 
>> I have tried extracting the hours which are 0 and converting them to
>24,
>> but
>> then I can't paste them back in the date/time of the original
>data.fram
>> again.
>> 
>> Are there maybe other solutions?
>> 
>> Thanks in advance,
>> 
>> Sandy
>> 
>> ifelse (as.POSIXlt(mydata[24,1])$hour =
>"0",as.POSIXlt(mydata[24,1])$hour
>> =
>> "24"
>> 
>
>Sandy,
>
>You really haven't given us enough information to provide a "solution",
>but here are some questions and suggestions.  
>
>Do you have any times less than 01:00:00 ?  You mention going from
>01:00:00 to 24:00:00 in you data.  I presume these are text fields and
>not time objects. Do you have fractional hours represented in your
>data, or are all times on the hour?
>
>1.  If your times are always on the hour no minutes or second, i.e.
>01:00 to 24:00, then you could read them as is and then just subtract 1
>hour from all date/time values.
>
>2.  If you have fractional hours, e.g. 00:32:00 or 11:45, then you
>could possible just read the date/time values and whenever the time is
>exactly 00:00:00, subtract 1 second from the value.  this will at least
>get you just before midnight on the previous day.
>
>Whether either of these approaches will work for you depends on what
>your actual needs are.  If this doesn't work for you, you will need to
>write back to R-help and explain more about what your actual needs are,
>and and provide more detail about you actual dates and times (see
>questions above".
>
>
>Hope this is somewhat helpful,
>
>Dan
>
>Daniel Nordlund
>Bothell, WA USA
>
>______________________________________________
>R-help at r-project.org 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.



More information about the R-help mailing list