[R] date and time coding question

MacQueen, Don macqueen1 at llnl.gov
Wed May 15 21:44:11 CEST 2013


I think you probably want
   format='%m/%d/%y %H:%M')
(lower case "y")

diff() as suggested by Jeff Newmiller is good, except that I don't know
how to control the units using diff().

## so a method that allows specifying units other than hours would be, for
example, 

datetime <-c("1/1/13 00:00","1/1/13 12:00","1/2/13 00:00","1/2/13 12:00")
datetime <-as.POSIXct(datetime,format='%m/%d/%y %H:%M')deltas <- difftime(
datetime[-1], datetime[-length(datetime)] , units='min')

-Don


-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 5/10/13 10:24 AM, "Andras Farkas" <motyocska at yahoo.com> wrote:

>Dear All
> 
>I have the following code:
> 
>datetime <-c("1/1/13 00:00","1/1/13 12:00","1/2/13 00:00","1/2/13 12:00")
>datetime <-as.POSIXct(datetime,format='%m/%d/%Y %H:%M')
>times 
><-matrix(c(difftime(datetime[2],datetime[1],units="hours"),difftime(dateti
>me[3],datetime[2],units="hours"),difftime(datetime[4],datetime[3],units="h
>ours")))
>
>I would like to aks for some directions on how to make the 'times' object
>code a bit more "uniform" to accept differenth lenths of 'datetime'...
>Occasionally I have the 'datetime' object include up to 15 date and time
>points, but that code limits the system from accepting a longer
>'datetime' object. This is what that looks like (basically same as above,
>but much longer): 
> 
>datetime <-c("1/1/13 00:00","1/1/13 12:00","1/2/13 00:00","1/2/13 12:00")
>datetime <-as.POSIXct(datetime,format='%m/%d/%Y %H:%M')
>times 
><-matrix(c(difftime(datetime[2],datetime[1],units="hours"),difftime(dateti
>me[3],datetime[2],units="hours"),difftime(datetime[4],datetime[3],units="h
>ours"),difftime(datetime[5],datetime[4],units="hours"),difftime(datetime[6
>],datetime[5],units="hours"),difftime(datetime[7],datetime[6],units="hours
>"),difftime(datetime[8],datetime[7],units="hours"),difftime(datetime[9],da
>tetime[8],units="hours"),difftime(datetime[10],datetime[9],units="hours"),
>difftime(datetime[11],datetime[10],units="hours"),difftime(datetime[12],da
>tetime[11],units="hours"),difftime(datetime[13],datetime[12],units="hours"
>),difftime(datetime[14],datetime[13],units="hours"),difftime(datetime[15],
>datetime[14],units="hours"),difftime(datetime[16],datetime[15],units="hour
>s")))
>
>appreciate the help,
> 
>Andras
>	[[alternative HTML version deleted]]
>



More information about the R-help mailing list