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