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.
C W
2019-Feb-02 16:51 UTC
[R] Why is there error in as.POSIXlt.character when using strftime()?
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 at 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 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. >[[alternative HTML version deleted]]
Jeff Newmiller
2019-Feb-02 17:02 UTC
[R] Why is there error in as.POSIXlt.character when using strftime()?
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 <tmrsg11 at 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 at 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 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. >>-- Sent from my phone. Please excuse my brevity.
C W
2019-Feb-02 17:03 UTC
[R] Why is there error in as.POSIXlt.character when using strftime()?
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 at 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 at 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 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. >> >[[alternative HTML version deleted]]