[R] Reading time/date string

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Aug 9 09:27:54 CEST 2007


On Thu, 9 Aug 2007, Matthew Walker wrote:

> Thanks Mark, that was very helpful.  I'm now sooooo close!
>
> Can anyone tell me how to extract the "value" from an instance of a
> "difftime" class?  I can see the value, but how can I place it in a
> dataframe?

as.numeric(time_delta)

Hint: you want the number, not the value (which is a classed object).

>
> > time_string1 <- "10:17:07 02 Aug 2007"
> > time_string2 <- "13:17:40 02 Aug 2007"
> >
> > time1 <- strptime(time_string1, format="%H:%M:%S %d %b %Y")
> > time2 <- strptime(time_string2, format="%H:%M:%S %d %b %Y")
> >
> > time_delta <- difftime(time2,time1, unit="sec")
> > time_delta
> Time difference of 10833 secs     # <--- I'd like this value just here!
> >
> > data.frame(time1, time2, time_delta)
> Error in as.data.frame.default(x[[i]], optional = TRUE) :
>        cannot coerce class "difftime" into a data.frame
>
>
>
> Thanks again,
>
> Matthew
>
>
> Mark W Kimpel wrote:
>> Look at some of these functions...
>>
>> DateTimeClasses(base)   Date-Time Classes
>> as.POSIXct(base)        Date-time Conversion Functions
>> cut.POSIXt(base)        Convert a Date or Date-Time Object to a Factor
>> format.Date(base)       Date Conversion Functions to and from Character
>>
>> Mark
>> ---
>>
>> Mark W. Kimpel MD  ** Neuroinformatics ** Dept. of Psychiatry
>> Indiana University School of Medicine
>>
>> 15032 Hunter Court, Westfield, IN  46074
>>
>> (317) 490-5129 Work, & Mobile & VoiceMail
>> (317) 663-0513 Home (no voice mail please)
>>
>> ******************************************************************
>>
>> Matthew Walker wrote:
>>> Hello everyone,
>>>
>>> Can anyone tell me what function I should use to read time/date
>>> strings and turn them into a form such that I can easily calculate
>>> the difference of two?  The strings I've got look like "10:17:07 02
>>> Aug 2007".  If I could calculate the number of seconds between them
>>> I'd be very happy!
>>>
>>> Cheers,
>>>
>>> Matthew
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch 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 stat.math.ethz.ch 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



More information about the R-help mailing list