[R] Time differences (as.difftime?) issue
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed May 14 17:34:09 CEST 2008
On Wed, 14 May 2008, Lukas Rode wrote:
> Dear all,
>
> I have a vector generated using the function strptime:
>
>> my.dt
> [1] "2004-04-19 08:35:00 W. Europe Daylight Time" "2004-04-19 09:35:00 W.
> Europe Daylight Time" "2004-04-19 11:35:00 W. Europe Daylight Time"
> [4] "2004-04-19 13:35:00 W. Europe Daylight Time" "2004-04-20 07:50:00 W.
> Europe Daylight Time"
>> class(my.dt)
> [1] "POSIXt" "POSIXct"
Somehow you didn't as that is not the class of a result from strptime.
> Now I would like to test if the difference from the times to the first time
> is lower than a given threshold:
>> my.dt - my.dt[1] < threshold
>
> The issue is, I would like to specificy the threshold as flexibly as
> possible, and I tried to do this using as.difftime
>> as.difftime(23, '%H')
> Time difference of 23 hours
>
> However, I cannot specify a threshold larger than 23 hours in units of
> hours:
>> as.difftime(24, '%H')
> Time difference of NA secs
It's much simpler to use
> as.difftime(47, units="hours")
Time difference of 47 hours
>
> I can hack around this in the following way:
>> threshold <- as.difftime(23, '%H')
>> threshold
> Time difference of 23 hours
>> threshold[[1]] <- 47
>> threshold
> Time difference of 47 hours
>
> In that way I can use my original statement:
>> my.dt - my.dt[1] < threshold
> [1] TRUE TRUE TRUE TRUE TRUE
>
> In summary, my question is: Is there a more elegant way than my hack from
> above to specify time difference thresholds of for example 47 hours, or of
> 246 seconds, or of 33 days and 5 hours? This could be using as.difftime or
> using other means.
>
> Thanks in advance,
> Lukas
>
> P.S.
>> sessionInfo()
> R version 2.4.1 (2006-12-18)
> i386-pc-mingw32
>
> locale:
> LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
> States.1252;LC_MONETARY=English_United
> States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252
>
> attached base packages:
> [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
> "base"
>
> other attached packages:
> chron odfWeave XML gplots gdata
> gtools RColorBrewer lattice foreign nlme
> "2.3-9" "0.7.3" "1.4-0" "2.3.2" "2.3.1"
> "2.3.0" "0.2-3" "0.14-16" "0.8-18" "3.1-79"
--
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