[R] Why is there error in as.POSIXlt.character when using strftime()?

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Sat Feb 2 16:09:46 CET 2019


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



More information about the R-help mailing list