[R] date and time coding question

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Fri May 10 19:56:59 CEST 2013


a) Please read the Posting Guide and post in plain text as it says to do. This is an adjustment you must make in your email client software, we cannot advise you how to do it here.

b) Not clear why you want a matrix. A vector is more typical in R; most of the time using n*1 matrices is an unnecessary Matlab habit.

c) See ?diff

diff(datetime)

or if you definitely need it:

matrix(diff(datetime),ncol=1)

---------------------------------------------------------------------------
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.

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(datetime[3],datetime[2],units="hours"),difftime(datetime[4],datetime[3],units="hours")))
>
>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(datetime[3],datetime[2],units="hours"),difftime(datetime[4],datetime[3],units="hours"),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],datetime[8],units="hours"),difftime(datetime[10],datetime[9],units="hours"),difftime(datetime[11],datetime[10],units="hours"),difftime(datetime[12],datetime[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="hours")))
>
>appreciate the help,
>>Andras
>	[[alternative HTML version deleted]]
>
>
>
>------------------------------------------------------------------------
>
>______________________________________________
>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