[R] Date Time conversion

Gabor Grothendieck ggrothendieck at gmail.com
Fri Sep 26 16:43:01 CEST 2008


chron uses:

- separate arguments for date and time -- not a single one as you have
- a separate system for the codes, not the % codes of Date and POSIXt classes

If you want to specify the date and time all as one string
rather than two arguments, use as.chron instead of
chron -- as.chron does use the same percent codes
as Date and POSIXct.  Notice further that the percent
codes used in the question were wrong with respect to
hours and minutes and should be as follows:

as.chron(as.character(f), "%m/%d/%y %H:%M")

Suggest you spend a few minutes going over the relevant
article in R News 4/1 -- although that does not cover as.chron
used in the above way as that was added a few years after
the article was written.


On Fri, Sep 26, 2008 at 10:18 AM, stephen sefick <ssefick at gmail.com> wrote:
> what am I doing wrong?
>
> chron(as.character(f), format=c(dates="%m/%d/%y", times="%h:%m"))
>
> f <- structure(c(51L, 60L, 66L, 87L, 90L, 115L, 23L, 35L, 37L, 6L,
> 12L, 55L, 84L, 96L, 109L, 17L, 29L, 41L, 3L, 74L, 94L, 102L,
> 30L, 8L, 46L, 69L, 107L, 15L, 25L, 39L, 1L, 71L, 95L, 19L, 56L,
> 62L, 76L, 85L, 99L, 111L, 42L, 4L, 52L, 61L, 67L, 91L, 13L, 24L,
> 36L, 38L, 7L, 81L, 82L, 57L, 64L, 100L, 113L, 21L, 33L, 44L,
> 78L, 54L, 73L, 83L, 108L, 16L, 28L, 2L, 58L, 65L, 86L, 101L,
> 32L, 43L, 77L, 93L, 104L, 10L, 48L, 53L, 97L, 110L, 18L, 27L,
> 75L, 92L, 103L, 31L, 9L, 47L, 70L, 72L, 106L, 14L, 26L, 40L,
> 79L, 98L, 112L, 68L, 88L, 105L, 20L, 11L, 49L, 63L, 5L, 80L,
> 50L, 59L, 89L, 114L, 22L, 34L, 45L), .Label = c("1/14/08 12:26",
> "1/15/08 15:25", "1/15/08 16:32", "1/16/08 11:36", "1/16/08 15:29",
> "1/21/08 11:21", "1/21/08 14:18", "1/22/07 13:59", "1/22/07 15:39",
> "1/22/07 18:17", "1/23/07 17:44", "1/23/07 9:17", "10/1/07 13:52",
> "10/22/07 14:38", "10/22/07 15:45", "10/23/07 15:30", "10/23/07 16:44",
> "10/23/07 9:35", "10/25/06 15:29", "10/25/06 17:10", "10/25/07 11:44",
> "10/26/07 8:58", "10/27/07 14:28", "10/29/07 14:43", "11/12/07 10:13",
> "11/12/07 11:49", "11/12/07 13:44", "11/13/07 16:24", "11/13/07 17:23",
> "11/14/06 11:39", "11/14/06 13:10", "11/15/07 10:19", "11/15/07 12:58",
> "11/16/07 10:48", "11/19/07 12:39", "11/19/07 15:15", "12/10/07 12:54",
> "12/10/07 15:42", "12/3/07 11:54", "12/3/07 12:59", "12/4/07 17:45",
> "12/5/07 15:49", "12/6/07 10:52", "12/6/07 12:19", "12/7/07 10:41",
> "2/19/07 14:28", "2/19/07 16:47", "2/20/07 13:20", "2/21/07 9:22",
> "2/22/07 15:41", "2/23/07 14:29", "2/26/07 13:43", "3/13/07 12:51",
> "3/13/07 13:46", "3/13/07 14:47", "3/14/07 11:08", "3/14/07 14:43",
> "3/14/07 15:58", "3/15/07 14:21", "3/16/07 13:30", "3/18/07 11:43",
> "4/11/07 16:09", "4/11/07 17:24", "4/12/07 10:55", "4/12/07 9:15",
> "4/14/07 14:16", "4/16/07 14:31", "4/25/06 15:12", "4/9/07 12:53",
> "4/9/07 14:30", "5/14/07 12:33", "5/14/07 14:01", "5/15/07 15:21",
> "5/15/07 15:56", "5/15/07 8:40", "5/16/07 11:24", "5/16/07 13:14",
> "5/16/07 14:51", "5/16/07 8:03", "5/17/07 15:25", "5/18/07 16:44",
> "5/21/07 14:13", "7/10/07 16:16", "7/10/07 17:54", "7/11/07 15:08",
> "7/12/07 9:39", "7/14/07 13:10", "7/19/06 7:04", "8/10/07 9:32",
> "8/11/07 13:45", "8/13/07 14:27", "8/21/06 14:11", "8/22/06 10:59",
> "8/22/06 9:43", "8/23/06 9:52", "8/7/07 16:46", "8/7/07 9:51",
> "8/8/07 17:45", "8/8/07 8:25", "8/9/07 10:40", "8/9/07 9:27",
> "9/18/06 12:42", "9/18/06 13:56", "9/19/06 16:22", "9/20/06 17:12",
> "9/24/07 14:26", "9/24/07 16:36", "9/25/07 15:43", "9/25/07 16:42",
> "9/25/07 9:44", "9/26/07 12:26", "9/26/07 17:58", "9/27/07 10:46",
> "9/28/07 9:21", "9/29/07 14:55"), class = "factor")
> --
> Stephen Sefick
> Research Scientist
> Southeastern Natural Sciences Academy
>
> Let's not spend our time and resources thinking about things that are
> so little or so large that all they really do for us is puff us up and
> make us feel like gods.  We are mammals, and have not exhausted the
> annoying little problems of being mammals.
>
>                                                                -K. Mullis
>
> ______________________________________________
> 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