Hello R users I am searching for a descriptive summary of the use of POSIXlt as compared to POSIXct date/time formats. I have been using them extensively for different purposes, but still can't quite understand when to use which one for the most efficient coding and use. I typically use them in graphics, comparison of times, interpolation of values between times, computation of time-series parameters, and so on. My request is simply to learn if there is a resource out there that explains the strengths of the use of each format in different situations and if certain situations require one over the other. My web searches have turned up basic things like the vector form (POSIXlt) vs the decimal form (POSIXct), but I could not find specific guidance to understand when it is best to use one over the other. Thanks Steve
POSIXlt vs POSIXct
10 messages · Steven R Corsi, Duncan Murdoch, R. Michael Weylandt +3 more
On 28/03/2012 10:06 AM, Steven R Corsi wrote:
Hello R users I am searching for a descriptive summary of the use of POSIXlt as compared to POSIXct date/time formats. I have been using them extensively for different purposes, but still can't quite understand when to use which one for the most efficient coding and use. I typically use them in graphics, comparison of times, interpolation of values between times, computation of time-series parameters, and so on. My request is simply to learn if there is a resource out there that explains the strengths of the use of each format in different situations and if certain situations require one over the other. My web searches have turned up basic things like the vector form (POSIXlt) vs the decimal form (POSIXct), but I could not find specific guidance to understand when it is best to use one over the other.
The first of the "Other Topics" among the "Technical Papers" available from the main HTML help page in R should address this. Duncan Murdoch
1 day later
Thanks very much for the response. That was a very good article and gives me a good appreciation for the history and covers the structure of the two date/time formats well. What I was specifically looking for is a feel for the situations when one format should be used over the other. In my work, I have gotten the impression that I should just use POSIXct for general useability in functions and graphics until I need to extract specific date components such as month, day, year, etc. In those instances, just convert to POSIXlt and extract needed info. Is this mostly accurate? More generally, is there a resource that summarizes which date/time objects to use under which conditions? So far, I have mostly been learning by trial/error/web searching which eventually is effective, but can be quite slow. Thanks Steve =============================================== Steven R. Corsi Phone: (608) 821-3835 Research Hydrologist email: srcorsi at usgs.gov U.S. Geological Survey Wisconsin Water Science Center 8505 Research Way Middleton, WI 53562 ===============================================
On 3/28/2012 12:16 PM, Duncan Murdoch wrote:
On 28/03/2012 10:06 AM, Steven R Corsi wrote:
Hello R users I am searching for a descriptive summary of the use of POSIXlt as compared to POSIXct date/time formats. I have been using them extensively for different purposes, but still can't quite understand when to use which one for the most efficient coding and use. I typically use them in graphics, comparison of times, interpolation of values between times, computation of time-series parameters, and so on. My request is simply to learn if there is a resource out there that explains the strengths of the use of each format in different situations and if certain situations require one over the other. My web searches have turned up basic things like the vector form (POSIXlt) vs the decimal form (POSIXct), but I could not find specific guidance to understand when it is best to use one over the other.
The first of the "Other Topics" among the "Technical Papers" available from the main HTML help page in R should address this. Duncan Murdoch
That's generally my reaction to them, but you should also read "R News 4/1 -- Help Desk" (http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf) which gives some tips on Date()s and the various time classes. Best, Michael
On Thu, Mar 29, 2012 at 2:19 PM, Steven R Corsi <srcorsi at usgs.gov> wrote:
Thanks very much for the response. That was a very good article and gives me a good appreciation for the history and covers the structure of the two date/time formats well. What I was specifically looking for is a feel for the situations when one format should be used over the other. In my work, I have gotten the impression that I should just use POSIXct for general useability in functions and graphics until I need to extract specific date components such as month, day, year, etc. In those instances, just convert to POSIXlt and extract needed info. Is this mostly accurate? More generally, is there a resource that summarizes which date/time objects to use under which conditions? So far, I have mostly been learning by trial/error/web searching which eventually is effective, but can be quite slow. Thanks Steve =============================================== Steven R. Corsi ? ? ? ?Phone: (608) 821-3835 Research Hydrologist ? email: srcorsi at usgs.gov U.S. Geological Survey Wisconsin Water Science Center 8505 Research Way Middleton, WI 53562 =============================================== On 3/28/2012 12:16 PM, Duncan Murdoch wrote:
On 28/03/2012 10:06 AM, Steven R Corsi wrote:
Hello R users I am searching for a descriptive summary of the use of POSIXlt as compared to POSIXct date/time formats. I have been using them extensively for different purposes, but still can't quite understand when to use which one for the most efficient coding and use. I typically use them in graphics, comparison of times, interpolation of values between times, computation of time-series parameters, and so on. My request is simply to learn if there is a resource out there that explains the strengths of the use of each format in different situations and if certain situations require one over the other. My web searches have turned up basic things like the vector form (POSIXlt) vs the decimal form (POSIXct), but I could not find specific guidance to understand when it is best to use one over the other.
The first of the "Other Topics" among the "Technical Papers" available from the main HTML help page in R should address this. Duncan Murdoch
______________________________________________ R-help at r-project.org mailing list 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.
Thanks for the link Michael. This is a very good explanation with some very useful tips on which date classes to use for different purposes. It generally strengthened the concept that POSIXct is the way to go unless you need to extract specific components of the date from POSIXlt. Since strptime() appears to be the primary conversion route from character class with dates/times/time zones to a date/time class, and since strptime() results in a POSIXlt format, that was what I was commonly using in past applications. That format, at times, gave me errors in situations where I didn't expect them. Now it is clear that the second step of conversion to POSIXct is preferred for many purposes. Best Regards Steve =============================================== Steven R. Corsi Phone: (608) 821-3835 Research Hydrologist email: srcorsi at usgs.gov U.S. Geological Survey Wisconsin Water Science Center 8505 Research Way Middleton, WI 53562 ===============================================
On 3/29/2012 1:28 PM, R. Michael Weylandt wrote:
That's generally my reaction to them, but you should also read "R News 4/1 -- Help Desk" (http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf) which gives some tips on Date()s and the various time classes. Best, Michael On Thu, Mar 29, 2012 at 2:19 PM, Steven R Corsi<srcorsi at usgs.gov> wrote:
Thanks very much for the response. That was a very good article and gives me a good appreciation for the history and covers the structure of the two date/time formats well. What I was specifically looking for is a feel for the situations when one format should be used over the other. In my work, I have gotten the impression that I should just use POSIXct for general useability in functions and graphics until I need to extract specific date components such as month, day, year, etc. In those instances, just convert to POSIXlt and extract needed info. Is this mostly accurate? More generally, is there a resource that summarizes which date/time objects to use under which conditions? So far, I have mostly been learning by trial/error/web searching which eventually is effective, but can be quite slow. Thanks Steve =============================================== Steven R. Corsi Phone: (608) 821-3835 Research Hydrologist email: srcorsi at usgs.gov U.S. Geological Survey Wisconsin Water Science Center 8505 Research Way Middleton, WI 53562 =============================================== On 3/28/2012 12:16 PM, Duncan Murdoch wrote:
On 28/03/2012 10:06 AM, Steven R Corsi wrote:
Hello R users I am searching for a descriptive summary of the use of POSIXlt as compared to POSIXct date/time formats. I have been using them extensively for different purposes, but still can't quite understand when to use which one for the most efficient coding and use. I typically use them in graphics, comparison of times, interpolation of values between times, computation of time-series parameters, and so on. My request is simply to learn if there is a resource out there that explains the strengths of the use of each format in different situations and if certain situations require one over the other. My web searches have turned up basic things like the vector form (POSIXlt) vs the decimal form (POSIXct), but I could not find specific guidance to understand when it is best to use one over the other.
The first of the "Other Topics" among the "Technical Papers" available from the main HTML help page in R should address this. Duncan Murdoch
______________________________________________ R-help at r-project.org mailing list 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.
I also find that POSIXct is generally the most useful, and only use POSIXlt in special cases. But have you considered as.POSIXct() instead of strptime()? It works for me, and I can't remember the last time I had to use strptime() for converting character to date/time. (But I mostly don't work with multiple time zones, except for converting to/from UTC. -Don
Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 3/29/12 12:11 PM, "Steven R Corsi" <srcorsi at usgs.gov> wrote: >Thanks for the link Michael. This is a very good explanation with some >very useful tips on which date classes to use for different purposes. It >generally strengthened the concept that POSIXct is the way to go unless >you need to extract specific components of the date from POSIXlt. Since >strptime() appears to be the primary conversion route from character >class with dates/times/time zones to a date/time class, and since >strptime() results in a POSIXlt format, that was what I was commonly >using in past applications. That format, at times, gave me errors in >situations where I didn't expect them. Now it is clear that the second >step of conversion to POSIXct is preferred for many purposes. > >Best Regards >Steve > >=============================================== >Steven R. Corsi Phone: (608) 821-3835 >Research Hydrologist email: srcorsi at usgs.gov >U.S. Geological Survey >Wisconsin Water Science Center >8505 Research Way >Middleton, WI 53562 >=============================================== > > >On 3/29/2012 1:28 PM, R. Michael Weylandt wrote: >> That's generally my reaction to them, but you should also read "R News >> 4/1 -- Help Desk" >> (http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf) which gives >> some tips on Date()s and the various time classes. >> >> Best, Michael >> >> On Thu, Mar 29, 2012 at 2:19 PM, Steven R Corsi<srcorsi at usgs.gov> >>wrote: >>> Thanks very much for the response. That was a very good article and >>>gives me >>> a good appreciation for the history and covers the structure of the two >>> date/time formats well. >>> >>> What I was specifically looking for is a feel for the situations when >>>one >>> format should be used over the other. In my work, I have gotten the >>> impression that I should just use POSIXct for general useability in >>> functions and graphics until I need to extract specific date >>>components such >>> as month, day, year, etc. In those instances, just convert to POSIXlt >>>and >>> extract needed info. Is this mostly accurate? More generally, is there >>>a >>> resource that summarizes which date/time objects to use under which >>> conditions? So far, I have mostly been learning by trial/error/web >>>searching >>> which eventually is effective, but can be quite slow. >>> >>> Thanks >>> Steve >>> >>> =============================================== >>> Steven R. Corsi Phone: (608) 821-3835 >>> Research Hydrologist email: srcorsi at usgs.gov >>> U.S. Geological Survey >>> Wisconsin Water Science Center >>> 8505 Research Way >>> Middleton, WI 53562 >>> =============================================== >>> >>> >>> >>> On 3/28/2012 12:16 PM, Duncan Murdoch wrote: >>>> On 28/03/2012 10:06 AM, Steven R Corsi wrote: >>>>> Hello R users >>>>> >>>>> I am searching for a descriptive summary of the use of POSIXlt as >>>>> compared to POSIXct date/time formats. I have been using them >>>>> extensively for different purposes, but still can't quite understand >>>>> when to use which one for the most efficient coding and use. I >>>>>typically >>>>> use them in graphics, comparison of times, interpolation of values >>>>> between times, computation of time-series parameters, and so on. >>>>> >>>>> My request is simply to learn if there is a resource out there that >>>>> explains the strengths of the use of each format in different >>>>>situations >>>>> and if certain situations require one over the other. My web searches >>>>> have turned up basic things like the vector form (POSIXlt) vs the >>>>> decimal form (POSIXct), but I could not find specific guidance to >>>>> understand when it is best to use one over the other. >>>> >>>> The first of the "Other Topics" among the "Technical Papers" available >>>> from the main HTML help page in R should address this. >>>> >>>> Duncan Murdoch >>> >>> ______________________________________________ >>> R-help at r-project.org mailing list >>> 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. > >______________________________________________ >R-help at r-project.org mailing list >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.
That is a good tip. I have tried it and found that it works if I make sure it was converted to character first. strptime appears to accept factor variables which is the default when reading in the file originally. Thanks Steve
On 3/29/2012 2:31 PM, MacQueen, Don wrote:
I also find that POSIXct is generally the most useful, and only use POSIXlt in special cases. But have you considered as.POSIXct() instead of strptime()? It works for me, and I can't remember the last time I had to use strptime() for converting character to date/time. (But I mostly don't work with multiple time zones, except for converting to/from UTC. -Don
On 29/03/2012 21:05, Steven R Corsi wrote:
That is a good tip. I have tried it and found that it works if I make sure it was converted to character first. strptime appears to accept factor variables which is the default when reading in the file originally.
I would say that POSIXct is the way to go if you know the timezone of the datetimes (it is essential to do the conversion). But you don't always do so, and then POSIXlt can be useful (although assuming UTC can also work). Several times a month we get help requests about times on DST transitions which shows that people too often think they know the timezone and in fact do not (as the times are invalid or ambiguous in the assumed timezone). as.POSIXct does work with factors .... z <- "2012-03-29 21:20:05" > as.POSIXct(z) [1] "2012-03-29 21:20:05 BST" > as.POSIXct(factor(z)) [1] "2012-03-29 21:20:05 BST"
Thanks Steve On 3/29/2012 2:31 PM, MacQueen, Don wrote:
I also find that POSIXct is generally the most useful, and only use POSIXlt in special cases. But have you considered as.POSIXct() instead of strptime()? It works for me, and I can't remember the last time I had to use strptime() for converting character to date/time. (But I mostly don't work with multiple time zones, except for converting to/from UTC. -Don
______________________________________________ R-help at r-project.org mailing list 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.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120329/80df0162/attachment.pl>
1 day later
On 29.03.2012 23:44, Steven R Corsi wrote:
Thanks for this feedback. I was able to get that example to work, but I
have gotten conflicting results with factors converted via POSIXct using
the format= and tz= options to bring in excel-type dates and times. Here
is what I have tried:
dt.factor<- factor(c("3/17/2011 14:07","3/20/2011 7:18","3/22/2011
14:10","3/29/2011 9:07","7/15/2010 7:49"))
dt.factor
[1] 3/17/2011 14:07 3/20/2011 7:18 3/22/2011 14:10 3/29/2011 9:07 7/15/2010 7:49 Levels: 3/17/2011 14:07 3/20/2011 7:18 3/22/2011 14:10 3/29/2011 9:07 7/15/2010 7:49
as.POSIXct(dt.factor,format="%m/%d/%Y %H:%M",tz="CST6CDT")
Error in as.POSIXlt.character(as.character(x)) :
character string is not in a standard unambiguous format
Works for me in R-release. Uwe Ligges
as.POSIXct(as.character(dt.factor),format="%m/%d/%Y %H:%M",tz="CST6CDT")
[1] "2011-03-17 14:07:00 CDT" "2011-03-20 07:18:00 CDT" "2011-03-22 14:10:00 CDT" [4] "2011-03-29 09:07:00 CDT" "2010-07-15 07:49:00 CDT" So, it works only after converting the factor variable to character, but it is a simple matter to do that conversion. Thanks Steve On 3/29/2012 3:24 PM, Prof Brian Ripley wrote:
On 29/03/2012 21:05, Steven R Corsi wrote:
That is a good tip. I have tried it and found that it works if I make sure it was converted to character first. strptime appears to accept factor variables which is the default when reading in the file originally.
I would say that POSIXct is the way to go if you know the timezone of the datetimes (it is essential to do the conversion). But you don't always do so, and then POSIXlt can be useful (although assuming UTC can also work). Several times a month we get help requests about times on DST transitions which shows that people too often think they know the timezone and in fact do not (as the times are invalid or ambiguous in the assumed timezone). as.POSIXct does work with factors .... z<- "2012-03-29 21:20:05"
as.POSIXct(z)
[1] "2012-03-29 21:20:05 BST"
as.POSIXct(factor(z))
[1] "2012-03-29 21:20:05 BST"
Thanks Steve On 3/29/2012 2:31 PM, MacQueen, Don wrote:
I also find that POSIXct is generally the most useful, and only use POSIXlt in special cases. But have you considered as.POSIXct() instead of strptime()? It works for me, and I can't remember the last time I had to use strptime() for converting character to date/time. (But I mostly don't work with multiple time zones, except for converting to/from UTC. -Don
______________________________________________ R-help at r-project.org mailing list 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]]
______________________________________________ R-help at r-project.org mailing list 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.