[R] chron - when seconds data not included

Gabor Grothendieck ggrothendieck at gmail.com
Tue Dec 9 06:29:41 CET 2008


On Mon, Dec 8, 2008 at 11:52 PM, Tubin <sredmonson at yahoo.com> wrote:
>
> I have date and time data which looks like this:
>
>      [,1]     [,2]
>  [1,] "7/1/08" "9:19"
>  [2,] "7/1/08" "9:58"
>  [3,] "7/7/08" "15:47"
>  [4,] "7/8/08" "10:03"
>  [5,] "7/8/08" "10:32"
>  [6,] "7/8/08" "15:22"
>  [7,] "7/8/08" "15:27"
>  [8,] "7/8/08" "15:40"
>  [9,] "7/9/08" "10:25"
> [10,] "7/9/08" "10:27"
>
> I would like to use chron on it, so that I can calculate intervals in time.
>
> I can't seem to get chron to accept the time format that doesn't include
> seconds.  Do I have to go through and append :00 on every line in order to
> use chron?



That's one way:

m <- matrix( c("7/1/08","9:19",
  "7/1/08","9:58",
  "7/7/08","15:47",
  "7/8/08","10:03",
  "7/8/08","10:32",
  "7/8/08","15:22",
  "7/8/08","15:27",
  "7/8/08","15:40",
  "7/9/08","10:25",
  "7/9/08","10:27"), nc = 2, byrow = TRUE)

chron(m[,1], paste(m[,2], 0, sep = ":"))

# another is to use as.chron

as.chron(apply(m, 1, paste, collapse = " "), "%m/%d/%y %H:%M")



More information about the R-help mailing list