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

C W tmr@g11 @end|ng |rom gm@||@com
Sun Feb 3 02:19:47 CET 2019


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 <jdnewmil using dcn.davis.ca.us>
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 <tmrsg11 using gmail.com> 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 <tmrsg11 using gmail.com> 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
> ><jdnewmil using dcn.davis.ca.us>
> >> 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 <
> >>> murdoch.duncan using 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 using 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.
> >>>
> >>
>
> --
> Sent from my phone. Please excuse my brevity.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list