C W
2019-Feb-02 03:45 UTC
[R] Why is there error in as.POSIXlt.character when using strftime()?
Dear R community, I am working with dates. And I get the following error:> strftime(dat[20], format="%H:%M")Error in as.POSIXlt.character(as.character(x), ...) : character string is not in a standard unambiguous format Here's the original data: dat <- structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 20L, 21L, 22L), .Label = c("7/12/15 11:32", "7/12/15 11:42", "7/12/15 12:17", "7/12/15 12:31", "7/12/15 12:50", "7/12/15 14:10", "7/12/15 14:19", "7/12/15 14:59", "7/12/15 15:57", "7/12/15 16:00", "7/12/15 16:46", "7/12/15 16:51", "7/12/15 17:35", "7/12/15 17:59", "7/12/15 18:17", "7/12/15 19:07", "7/12/15 19:08", "7/12/15 19:31", "7/12/15 21:21", "7/13/15 10:01", "7/13/15 10:03", "7/13/15 10:05", "7/13/15 6:00", "7/13/15 6:20", "7/13/15 6:37", "7/13/15 6:40", "7/13/15 6:46", "7/13/15 7:20", "7/13/15 7:47", "7/13/15 7:50", "7/13/15 7:54", "7/13/15 8:11", "7/13/15 8:23", "7/13/15 8:31", "7/13/15 8:33", "7/13/15 8:43", "7/13/15 9:04", "7/13/15 9:09", "7/13/15 9:30", "7/13/15 9:59"), class = "factor") It seems like things work fine for the first 19 elements,> strftime(dat[1:19], format="%H:%M")[1] "11:32" "11:42" "12:17" "12:31" "12:50" "14:10" "14:19" "14:59" [9] "15:57" "16:00" "16:46" "16:51" "17:35" "17:59" "18:17" "19:07" [17] "19:08" "19:31" "21:21" But why not element 20 and on? They *look* the same to me. What is going on? Thanks very much! [[alternative HTML version deleted]]
Duncan Murdoch
2019-Feb-02 15:09 UTC
[R] Why is there error in as.POSIXlt.character when using strftime()?
On 01/02/2019 10:45 p.m., C W wrote:> Dear R community, > > I am working with dates. And I get the following error: >> strftime(dat[20], format="%H:%M") > Error in as.POSIXlt.character(as.character(x), ...) : > character string is not in a standard unambiguous formatYou are using the wrong function: strftime() formats a time object as a character string. You want strptime() to convert character (or factor in your case) to a time object. But you need to give the format for the full string, not just the time at the end. If you really were intending to extract times from dat, then you need both conversions: > strftime(strptime(dat, format="%m/%d/%y %H:%M"), format = "%H:%M") [1] "11:32" "11:42" "12:17" "12:31" "12:50" "14:10" "14:19" "14:59" "15:57" "16:00" "16:46" "16:51" "17:35" "17:59" "18:17" "19:07" "19:08" [18] "19:31" "21:21" "06:00" "06:20" "06:37" "06:40" "06:46" "07:20" "07:47" "07:50" "07:54" "08:11" "08:23" "08:31" "08:33" "08:43" "09:04" [35] "09:09" "09:30" "09:59" "10:01" "10:03" "10:05" Duncan Murdoch
William Dunlap
2019-Feb-02 15:40 UTC
[R] Why is there error in as.POSIXlt.character when using strftime()?
Note that the first unparsable element is the first with a 13 in the second field, which is out of range for the month entry. If you look at the the whole date/time output by the first 19 elements you will see that you need to tell it the order of the year, month, and day> as.POSIXlt(dat[1:19])[1] "0007-12-15 11:32:00 LMT" "0007-12-15 11:42:00 LMT" [3] "0007-12-15 12:17:00 LMT" "0007-12-15 12:31:00 LMT" [5] "0007-12-15 12:50:00 LMT" "0007-12-15 14:10:00 LMT" [7] "0007-12-15 14:19:00 LMT" "0007-12-15 14:59:00 LMT" [9] "0007-12-15 15:57:00 LMT" "0007-12-15 16:00:00 LMT" [11] "0007-12-15 16:46:00 LMT" "0007-12-15 16:51:00 LMT" [13] "0007-12-15 17:35:00 LMT" "0007-12-15 17:59:00 LMT" [15] "0007-12-15 18:17:00 LMT" "0007-12-15 19:07:00 LMT" [17] "0007-12-15 19:08:00 LMT" "0007-12-15 19:31:00 LMT" [19] "0007-12-15 21:21:00 LMT"> as.POSIXlt(dat, format="%m/%d/%y %H:%M")[1] "2015-07-12 11:32:00 PDT" "2015-07-12 11:42:00 PDT" [3] "2015-07-12 12:17:00 PDT" "2015-07-12 12:31:00 PDT" [5] "2015-07-12 12:50:00 PDT" "2015-07-12 14:10:00 PDT" [7] "2015-07-12 14:19:00 PDT" "2015-07-12 14:59:00 PDT" [9] "2015-07-12 15:57:00 PDT" "2015-07-12 16:00:00 PDT" [11] "2015-07-12 16:46:00 PDT" "2015-07-12 16:51:00 PDT" [13] "2015-07-12 17:35:00 PDT" "2015-07-12 17:59:00 PDT" [15] "2015-07-12 18:17:00 PDT" "2015-07-12 19:07:00 PDT" [17] "2015-07-12 19:08:00 PDT" "2015-07-12 19:31:00 PDT" [19] "2015-07-12 21:21:00 PDT" "2015-07-13 06:00:00 PDT" [21] "2015-07-13 06:20:00 PDT" "2015-07-13 06:37:00 PDT" [23] "2015-07-13 06:40:00 PDT" "2015-07-13 06:46:00 PDT" [25] "2015-07-13 07:20:00 PDT" "2015-07-13 07:47:00 PDT" [27] "2015-07-13 07:50:00 PDT" "2015-07-13 07:54:00 PDT" [29] "2015-07-13 08:11:00 PDT" "2015-07-13 08:23:00 PDT" [31] "2015-07-13 08:31:00 PDT" "2015-07-13 08:33:00 PDT" [33] "2015-07-13 08:43:00 PDT" "2015-07-13 09:04:00 PDT" [35] "2015-07-13 09:09:00 PDT" "2015-07-13 09:30:00 PDT" [37] "2015-07-13 09:59:00 PDT" "2015-07-13 10:01:00 PDT" [39] "2015-07-13 10:03:00 PDT" "2015-07-13 10:05:00 PDT" Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Feb 2, 2019 at 6:46 AM C W <tmrsg11 at gmail.com> wrote:> Dear R community, > > I am working with dates. And I get the following error: > > strftime(dat[20], format="%H:%M") > Error in as.POSIXlt.character(as.character(x), ...) : > character string is not in a standard unambiguous format > > Here's the original data: > dat <- structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, > 13L, 14L, 15L, 16L, 17L, 18L, 19L, 23L, 24L, 25L, 26L, 27L, 28L, > 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 20L, > 21L, 22L), .Label = c("7/12/15 11:32", "7/12/15 11:42", "7/12/15 12:17", > "7/12/15 12:31", "7/12/15 12:50", "7/12/15 14:10", "7/12/15 14:19", > "7/12/15 14:59", "7/12/15 15:57", "7/12/15 16:00", "7/12/15 16:46", > "7/12/15 16:51", "7/12/15 17:35", "7/12/15 17:59", "7/12/15 18:17", > "7/12/15 19:07", "7/12/15 19:08", "7/12/15 19:31", "7/12/15 21:21", > "7/13/15 10:01", "7/13/15 10:03", "7/13/15 10:05", "7/13/15 6:00", > "7/13/15 6:20", "7/13/15 6:37", "7/13/15 6:40", "7/13/15 6:46", > "7/13/15 7:20", "7/13/15 7:47", "7/13/15 7:50", "7/13/15 7:54", > "7/13/15 8:11", "7/13/15 8:23", "7/13/15 8:31", "7/13/15 8:33", > "7/13/15 8:43", "7/13/15 9:04", "7/13/15 9:09", "7/13/15 9:30", > "7/13/15 9:59"), class = "factor") > > It seems like things work fine for the first 19 elements, > > strftime(dat[1:19], format="%H:%M") > [1] "11:32" "11:42" "12:17" "12:31" "12:50" "14:10" "14:19" "14:59" > [9] "15:57" "16:00" "16:46" "16:51" "17:35" "17:59" "18:17" "19:07" > [17] "19:08" "19:31" "21:21" > > But why not element 20 and on? They *look* the same to me. > > What is going on? > > Thanks very much! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Jeff Newmiller
2019-Feb-02 15:41 UTC
[R] Why is there error in as.POSIXlt.character when using strftime()?
... and in general, you need to specify the time zone to avoid surprises. In many cases this can be as simple as Sys.setenv(TZ="GMT") but it can be specific to your data set also. On February 2, 2019 7:09:46 AM PST, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:>On 01/02/2019 10:45 p.m., C W wrote: >> Dear R community, >> >> I am working with dates. And I get the following error: >>> strftime(dat[20], format="%H:%M") >> Error in as.POSIXlt.character(as.character(x), ...) : >> character string is not in a standard unambiguous format > >You are using the wrong function: strftime() formats a time object as >a >character string. You want strptime() to convert character (or factor >in your case) to a time object. > >But you need to give the format for the full string, not just the time >at the end. > >If you really were intending to extract times from dat, then you need >both conversions: > > > strftime(strptime(dat, format="%m/%d/%y %H:%M"), format = "%H:%M") > [1] "11:32" "11:42" "12:17" "12:31" "12:50" "14:10" "14:19" "14:59" >"15:57" "16:00" "16:46" "16:51" "17:35" "17:59" "18:17" "19:07" "19:08" >[18] "19:31" "21:21" "06:00" "06:20" "06:37" "06:40" "06:46" "07:20" >"07:47" "07:50" "07:54" "08:11" "08:23" "08:31" "08:33" "08:43" "09:04" >[35] "09:09" "09:30" "09:59" "10:01" "10:03" "10:05" > >Duncan Murdoch > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.-- Sent from my phone. Please excuse my brevity.