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

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

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

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Why is there error in as.POSIXlt.character when using strftime()?

Duncan Murdoch-2
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Why is there error in as.POSIXlt.character when using strftime()?

R help mailing list-2
In reply to this post by tmrsg11
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Why is there error in as.POSIXlt.character when using strftime()?

Jeff Newmiller
In reply to this post by Duncan Murdoch-2
... 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 <[hidden email]> 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
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Why is there error in as.POSIXlt.character when using strftime()?

tmrsg11
Thank you all very much, very helpful! :)

I'm just curious, what does strptime(), strftime(), as.POSIXlt.(), and
as.POSIXct() stand for?

On Sat, Feb 2, 2019 at 10:41 AM Jeff Newmiller <[hidden email]>
wrote:

> ... 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 <
> [hidden email]> 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
> >
> >______________________________________________
> >[hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Why is there error in as.POSIXlt.character when using strftime()?

Jeff Newmiller
strptime = string parse time (convert from string to POSIXlt, name of function as specified in POSIX standard)

strftime = string format time (make string, name of function as specified in POSIX standard)

as.POSIXlt = convert to POSIX list time format (same as strptime but name fits type conversion function naming pattern in R)

as.POSIXct = convert to POSIX continuous time (seconds since epoch, typically 1970-01-01 00:00:00 GMT, useful for computations)

?DateTimeClasses

On February 2, 2019 8:51:28 AM PST, C W <[hidden email]> wrote:

>Thank you all very much, very helpful! :)
>
>I'm just curious, what does strptime(), strftime(), as.POSIXlt.(), and
>as.POSIXct() stand for?
>
>On Sat, Feb 2, 2019 at 10:41 AM Jeff Newmiller
><[hidden email]>
>wrote:
>
>> ... 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 <
>> [hidden email]> 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
>> >
>> >______________________________________________
>> >[hidden email] 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.
>>

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Why is there error in as.POSIXlt.character when using strftime()?

tmrsg11
In reply to this post by tmrsg11
Also, I was able to extract date using
> as.Date(dat, "%m/%d/%y")
 [1] "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12"
 [6] "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12"
[11] "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12"
[16] "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-13"
[21] "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13"
[26] "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13"
[31] "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13"
[36] "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13"

> class(as.Date(dat, "%m/%d/%y"))
[1] "Date"
> class(strftime(strptime(dat, format="%m/%d/%y %H:%M"), format =
"%m/%d/%y"))
[1] "character"

If I want to date caluculation, would as.Date() be more desirable? Or does
strftime() do the same thing?



On Sat, Feb 2, 2019 at 11:51 AM C W <[hidden email]> wrote:

> Thank you all very much, very helpful! :)
>
> I'm just curious, what does strptime(), strftime(), as.POSIXlt.(), and
> as.POSIXct() stand for?
>
> On Sat, Feb 2, 2019 at 10:41 AM Jeff Newmiller <[hidden email]>
> wrote:
>
>> ... 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 <
>> [hidden email]> 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
>> >
>> >______________________________________________
>> >[hidden email] 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.
>>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Why is there error in as.POSIXlt.character when using strftime()?

Jeff Newmiller
If there are no time-of-day values in your data set the the Date type is great. However, it can be messy if you work with both Date and POSIXt types in the same analysis... I recommend sticking with one or the other.

The trunc.POSIXt function is more appropriate for getting POSIXt dates than converting to character and back.

On February 2, 2019 9:03:31 AM PST, C W <[hidden email]> wrote:

>Also, I was able to extract date using
>> as.Date(dat, "%m/%d/%y")
> [1] "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12"
> [6] "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12"
>[11] "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12"
>[16] "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-13"
>[21] "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13"
>[26] "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13"
>[31] "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13"
>[36] "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13"
>
>> class(as.Date(dat, "%m/%d/%y"))
>[1] "Date"
>> class(strftime(strptime(dat, format="%m/%d/%y %H:%M"), format =
>"%m/%d/%y"))
>[1] "character"
>
>If I want to date caluculation, would as.Date() be more desirable? Or
>does
>strftime() do the same thing?
>
>
>
>On Sat, Feb 2, 2019 at 11:51 AM C W <[hidden email]> wrote:
>
>> Thank you all very much, very helpful! :)
>>
>> I'm just curious, what does strptime(), strftime(), as.POSIXlt.(),
>and
>> as.POSIXct() stand for?
>>
>> On Sat, Feb 2, 2019 at 10:41 AM Jeff Newmiller
><[hidden email]>
>> wrote:
>>
>>> ... 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 <
>>> [hidden email]> 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
>>> >
>>> >______________________________________________
>>> >[hidden email] 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.
>>>
>>

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Why is there error in as.POSIXlt.character when using strftime()?

tmrsg11
Thanks lots Jeff, very clear explanation.

Yes, I think I will stick with the POSIXt family of functions.

On Sat, Feb 2, 2019 at 1:33 PM Jeff Newmiller <[hidden email]>
wrote:

> If there are no time-of-day values in your data set the the Date type is
> great. However, it can be messy if you work with both Date and POSIXt types
> in the same analysis... I recommend sticking with one or the other.
>
> The trunc.POSIXt function is more appropriate for getting POSIXt dates
> than converting to character and back.
>
> On February 2, 2019 9:03:31 AM PST, C W <[hidden email]> wrote:
> >Also, I was able to extract date using
> >> as.Date(dat, "%m/%d/%y")
> > [1] "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12"
> > [6] "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12"
> >[11] "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12"
> >[16] "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-12" "2015-07-13"
> >[21] "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13"
> >[26] "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13"
> >[31] "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13"
> >[36] "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13" "2015-07-13"
> >
> >> class(as.Date(dat, "%m/%d/%y"))
> >[1] "Date"
> >> class(strftime(strptime(dat, format="%m/%d/%y %H:%M"), format =
> >"%m/%d/%y"))
> >[1] "character"
> >
> >If I want to date caluculation, would as.Date() be more desirable? Or
> >does
> >strftime() do the same thing?
> >
> >
> >
> >On Sat, Feb 2, 2019 at 11:51 AM C W <[hidden email]> wrote:
> >
> >> Thank you all very much, very helpful! :)
> >>
> >> I'm just curious, what does strptime(), strftime(), as.POSIXlt.(),
> >and
> >> as.POSIXct() stand for?
> >>
> >> On Sat, Feb 2, 2019 at 10:41 AM Jeff Newmiller
> ><[hidden email]>
> >> wrote:
> >>
> >>> ... 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 <
> >>> [hidden email]> 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
> >>> >
> >>> >______________________________________________
> >>> >[hidden email] 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.
> >>>
> >>
>
> --
> Sent from my phone. Please excuse my brevity.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Why is there error in as.POSIXlt.character when using strftime()?

Peter Dalgaard-2
In reply to this post by Jeff Newmiller
* localtime format, I believe.

> On 2 Feb 2019, at 18:02 , Jeff Newmiller <[hidden email]> wrote:
>
> as.POSIXlt = convert to POSIX list time format (same as strptime but name fits type conversion function naming pattern in R)

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.