Hi list, Could someone explain to me why the following result is not a integer?> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004", >format="%d%b%Y"), units="days")Time difference of 195.0417 days I'm using R2.12.0 on WinXP. Thanks! ...Tao
Tao - The documentation for the difftime function says: Function ?difftime? calculates a difference of two date/time objects and returns an object of class ?"difftime"? with an attribute indicating the units. So that answers your question. If you want it to be an integer, you're certainly free to make it one:> as.integer(difftime(strptime("24NOV2004", format="%d%b%Y"),+ strptime("13MAY2004",format="%d%b%Y"), units="days")) [1] 195 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Fri, 29 Oct 2010, Shi, Tao wrote:> Hi list, > > Could someone explain to me why the following result is not a integer? > > >> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004", >> format="%d%b%Y"), units="days") > Time difference of 195.0417 days > > I'm using R2.12.0 on WinXP. > > Thanks! > > ...Tao > > ______________________________________________ > 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. >
Shi, Tao <shidaxia <at> yahoo.com> writes:> Could someone explain to me why the following result is not a integer? > > > difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004", > >format="%d%b%Y"), units="days") > Time difference of 195.0417 daysPresumably because this goes across a daylight-savings time adjustment? 0.0417=1/24 days is 1 hour ... Ben Bolker
Hi Phil, Thanks for the reply, but I don't think you have explained where the decimal part is coming from.... ...Tao ----- Original Message -----> From:Phil Spector <spector at stat.berkeley.edu> > To:"Shi, Tao" <shidaxia at yahoo.com> > Cc:r-help at stat.math.ethz.ch > Sent:Friday, October 29, 2010 5:04:23 PM > Subject:Re: [R] date calculation > > > Tao -The documentation for the difftime function> says:Function ?difftime? calculates a difference of two> date/timeobjects and returns an object of class> ?"difftime"? with anattribute indicating the> units.So that answers your question. If you want it to be an> integer, you're certainly free to make it one:> > as.integer(difftime(strptime("24NOV2004", format="%d%b%Y"),+> > strptime("13MAY2004",format="%d%b%Y"), units="days"))[1] 195> > - Phil Spector> Statistical Computing > Facility> Department of Statistics> > UC Berkeley> > ymailto="mailto:spector at stat.berkeley.edu" > href="mailto:spector at stat.berkeley.edu">spector at stat.berkeley.eduOn> Fri, 29 Oct 2010, Shi, Tao wrote:> Hi list, > > Could > someone explain to me why the following result is not a > integer? > > >> difftime(strptime("24NOV2004", > format="%d%b%Y"), strptime("13MAY2004", >> format="%d%b%Y"), > units="days") > Time difference of 195.0417 days > > I'm using > R2.12.0 on WinXP. > > Thanks! > > ...Tao > > > ______________________________________________ > > ymailto="mailto:R-help at r-project.org" > href="mailto:R-help at r-project.org">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. >
Hi Ben, That must be the case! In fact if I do:> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004",format="%d%b%Y"), units="days", tz="GMT")Time difference of 195 days which supports your claim. Can someone from the R development team confirm this? Thanks! ...Tao ----- Original Message -----> From:Ben Bolker <bbolker at gmail.com> > To:r-help at stat.math.ethz.ch > Cc: > Sent:Friday, October 29, 2010 7:54:53 PM > Subject:Re: [R] date calculation > > > Shi, Tao <shidaxia <at> > href="http://yahoo.com">yahoo.com> writes:> Could someone > explain to me why the following result is not a integer? > > > > difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004", > > >format="%d%b%Y"), units="days") > Time difference of 195.0417 > daysPresumably because this goes across a daylight-savings> timeadjustment? 0.0417=1/24 days is 1 hour ... Ben> Bolker______________________________________________> ymailto="mailto:R-help at r-project.org" > href="mailto:R-help at r-project.org">R-help at r-project.org mailing list> href="https://stat.ethz.ch/mailman/listinfo/r-help" target=_blank > >https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting> guide http://www.R-project.org/posting-guide.htmland provide commented,> minimal, self-contained, reproducible code.
On Fri, Oct 29, 2010 at 11:02 PM, Shi, Tao <shidaxia at yahoo.com> wrote:> Hi Ben, > > That must be the case! ?In fact if I do: > >> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004",format="%d%b%Y"), units="days", tz="GMT") > Time difference of 195 days > > > which supports your claim. > > Can someone from the R development team confirm this?Combining empirical results with the documentation (you can read more details about how timezones and dates are handled at ?DateTimeClasses ) which states that dst is used for relevant timezones (although this behavior can be OS dependent), this is not really necessary. For example, note that if one goes to a time before daylight savings:> difftime(strptime("24NOV1902", format="%d%b%Y"), strptime("13MAY1902",format="%d%b%Y"), units="days")Time difference of 195 days and that the hours match exactly what one would expect given dst.> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004",format="%d%b%Y"), units="hours")Time difference of 4681 hours> 195 * 24 + 1[1] 4681 In short, difftime() is performing as stated and documented (if slightly unexpected at first glance). Cheers, Josh> > Thanks! > > ...Tao > > > > > > ----- Original Message ----- >> From:Ben Bolker <bbolker at gmail.com> >> To:r-help at stat.math.ethz.ch >> Cc: >> Sent:Friday, October 29, 2010 7:54:53 PM >> Subject:Re: [R] date calculation >> >> >> Shi, Tao <shidaxia <at> >> href="http://yahoo.com">yahoo.com> writes: > >> Could someone >> explain to me why the following result is not a integer? >> >> > >> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004", >> >> >format="%d%b%Y"), units="days") >> Time difference of 195.0417 >> days > > ?Presumably because this goes across a daylight-savings >> time > adjustment? ?0.0417=1/24 days is 1 hour ... > > ?Ben >> Bolker > > ______________________________________________ > >> ymailto="mailto:R-help at r-project.org" >> href="mailto:R-help at r-project.org">R-help at r-project.org mailing list > >> href="https://stat.ethz.ch/mailman/listinfo/r-help" target=_blank >> >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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Hi Ben and Josh, No, I don't think that there is a bug. It's just due to my lack of knowledge on Date class, I was simply amazed that it has the daylight saving time built in at first. But on the second thought, it really should. Thank you all for the explanations! ...Tao From: Ben Bolker <bbolker@gmail.com> To: "Shi, Tao" <shidaxia@yahoo.com> Cc: "r-help@stat.math.ethz.ch" <r-help@stat.math.ethz.ch> Sent: Saturday, October 30, 2010 7:22:04 AM Subject: Re: [R] date calculation On 10-10-30 02:02 AM, Shi, Tao wrote:> Hi Ben, > > That must be the case! In fact if I do: > >> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004",format="%d%b%Y"), units="days", tz="GMT") > Time difference of 195 days > > > which supports your claim. > > Can someone from the R development team confirm this? > > Thanks! > > ...Tao > >It sounds like you think this is a bug. It's not (although it's arguably not what you want). The general advice when using dates and time in R is to use the *least* specific date format that will do what you want, i.e. don't use a format that incorporates time zone information (daylight savings time) information if you don't want to deal with these complexities. I would suggest the chron package: library(chron) diff(chron(dates.=c("24/11/2004","13/05/2004"),format="d/m/y"))> > > > ----- Original Message ----- >> From:Ben Bolker <bbolker@gmail.com> >> To:r-help@stat.math.ethz.ch >> Cc: >> Sent:Friday, October 29, 2010 7:54:53 PM >> Subject:Re: [R] date calculation >> >> >> Shi, Tao <shidaxia <at> >> href="http://yahoo.com">yahoo.com> writes: > >> Could someone >> explain to me why the following result is not a integer? >> >>> >> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004", >> >>> format="%d%b%Y"), units="days") >> Time difference of 195.0417 >> days > > Presumably because this goes across a daylight-savings >> time > adjustment? 0.0417=1/24 days is 1 hour ... > > Ben >> Bolker > > ______________________________________________ > >> ymailto="mailto:R-help@r-project.org" >> href="mailto:R-help@r-project.org">R-help@r-project.org mailing list > >> href="https://stat.ethz.ch/mailman/listinfo/r-help" target=_blank >>> 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]]
I think this demonstrate on of the differences between Class 'Date' and 'POSIXlt'. Thanks, Marc! ...Tao From: Marc Schwartz <marc_schwartz@me.com> To: Ben Bolker <bbolker@gmail.com> Cc: "Shi, Tao" <shidaxia@yahoo.com>; "r-help@stat.math.ethz.ch" <r-help@stat.math.ethz.ch> Sent: Saturday, October 30, 2010 7:48:05 AM Subject: Re: [R] date calculation On Oct 30, 2010, at 9:22 AM, Ben Bolker wrote:> On 10-10-30 02:02 AM, Shi, Tao wrote: >> Hi Ben, >> >> That must be the case! In fact if I do: >> >>> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004",format="%d%b%Y"), units="days", tz="GMT") >> Time difference of 195 days >> >> >> which supports your claim. >> >> Can someone from the R development team confirm this? >> >> Thanks! >> >> ...Tao >> >> > > It sounds like you think this is a bug. It's not (although it's > arguably not what you want). The general advice when using dates and > time in R is to use the *least* specific date format that will do what > you want, i.e. don't use a format that incorporates time zone > information (daylight savings time) information if you don't want to > deal with these complexities. > > I would suggest the chron package: > > library(chron) > diff(chron(dates.=c("24/11/2004","13/05/2004"),format="d/m/y"))Ben, If time and time zone are not relevant, any reason not to use:> diff(as.Date(c("13/05/2004", "24/11/2004"), format = "%d/%m/%Y"))Time difference of 195 days or perhaps conceptually easier: Date2 <- as.Date("24/11/2004", format = "%d/%m/%Y") Date1 <- as.Date("13/05/2004", format = "%d/%m/%Y")> Date2 - Date1Time difference of 195 days R has built in arithmetic operations for such dates, without the need to use another package, since they are effectively numerics with a Date class:> str(Date1)Class 'Date' num 12551> str(Date2)Class 'Date' num 12746 ? HTH, Marc Schwartz>> >> >> >> ----- Original Message ----- >>> From:Ben Bolker <bbolker@gmail.com> >>> To:r-help@stat.math.ethz.ch >>> Cc: >>> Sent:Friday, October 29, 2010 7:54:53 PM >>> Subject:Re: [R] date calculation >>> >>> >>> Shi, Tao <shidaxia <at> >>> href="http://yahoo.com">yahoo.com> writes: >> >>> Could someone >>> explain to me why the following result is not a integer? >>> >>>> >>> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004", >>> >>>> format="%d%b%Y"), units="days") >>> Time difference of 195.0417 >>> days >> >> Presumably because this goes across a daylight-savings >>> time >> adjustment? 0.0417=1/24 days is 1 hour ... >> >> Ben >>> Bolker[[alternative HTML version deleted]]