[R] The time unit of strptime() after performing arithmetic calculation

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Fri Aug 9 23:04:39 CEST 2013


The printed view of a difftime object is not necessarily reflective of its internal representation. Also, to avoid unpleasant surprises always use a conversion function with specified units (not "auto") if you want to convert between numeric and difftime.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Jun Shen <jun.shen.ut at gmail.com> wrote:
>Thanks Jim and David,
>
>The difftime() is exactly what I am looking for. Just out of curiosity
>why
>the unit of output is different. here is an example of the dataframe.
>
>test<-structure(list(SPDTC = c("2012-08-27T09:30", "2012-08-06T10:08",
>"2012-08-13T07:41", "2012-07-17T07:50", "2012-09-11T10:29"),
> TimeBL = c("2012-07-17T07:50", "2012-07-17T07:50", "2012-07-17T07:50",
>    "2012-07-17T07:50", "2012-07-17T07:50")), .Names = c("SPDTC",
>"TimeBL"), row.names = c(NA, 5L), class = "data.frame")
>
>Here is the calculation I did originally.
>
>>with(test,strptime(SPDTC,format='%Y-%m-%dT%H:%M')-strptime(TimeBL,format='%Y-%m-%dT%H:%M'))
>
>The result is in seconds.
>
>
>On Fri, Aug 9, 2013 at 1:55 PM, David Winsemius
><dwinsemius at comcast.net>wrote:
>
>>
>> On Aug 9, 2013, at 10:37 AM, Jun Shen wrote:
>>
>> > Hi all,
>> >
>> > I used strptime() to convert character strings to time and did some
>> > subtraction calculation.
>> >
>> > a<-'2012-07-17T07:50'
>> > b<-'2012-08-27T09:30'
>> >
>> >
>strptime(a,format='%Y-%m-%dT%H:%M')-strptime(b,format='%Y-%m-%dT%H:%M')
>> >
>> > The result shows
>> >
>> > Time difference of -41.06944 days.
>> >
>> > However when these operations performed in a dataframe, the time
>> difference
>> > is in the unit of seconds. Why is that? What I really want is in
>hours. I
>> > know I can convert the results manually to hours but just wonder if
>there
>> > is way to control the unit. Thanks.
>>
>> ?difftime
>> ?`-.POSIXt`
>>
>> Should provide exactly what is requested (and is linked from the
>> ?DateTimeClasses help page.)
>>
>> --
>> David Winsemius
>> Alameda, CA, USA
>>
>>
>
>	[[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.



More information about the R-help mailing list