[R] Selecting timestamps

Bill.Venables at csiro.au Bill.Venables at csiro.au
Thu Feb 21 08:40:00 CET 2008


Since both sequences are in hourly steps, there is a fairly easy way to
do this:

> before <- colSums(outer(seq1, seq2, "<"))
> sort(c(seq1[before], seq1[before+1]))  

This uses the fact that both sequences are in hourly time steps, so the
time stamp 'just after' the member of seq2 is the next one to the one
'just before'.  Relaxing this assumption, if needed, is left as an easy
exercise.

Bill Venables

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Lauri Nikkinen
Sent: Thursday, 21 February 2008 5:04 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Selecting timestamps

R-users,

I have two vectors (of timestamps)

d1 <- as.POSIXct(strptime("2.2.2002 07:00", format="%d.%m.%Y %H:%M"))
d2 <- as.POSIXct(strptime("4.2.2002 07:00", format="%d.%m.%Y %H:%M"))
seq1 <- seq(d1, d2, "hours")
seq1

d3 <- as.POSIXct(strptime("2.2.2002 15:22", format="%d.%m.%Y %H:%M"))
d4 <- as.POSIXct(strptime("3.2.2002 18:12", format="%d.%m.%Y %H:%M"))
seq2 <- seq(d3, d4, "hours")
seq2

How to select timestamps from seq1 which are just before and after of
particular timestamp in seq2?
The resulting vector should look like this in this example:

"2002-02-02 15:00:00 Normaaliaika"
"2002-02-02 16:00:00 Normaaliaika"
"2002-02-02 16:00:00 Normaaliaika"
"2002-02-02 17:00:00 Normaaliaika"
"2002-02-02 17:00:00 Normaaliaika"
"2002-02-02 18:00:00 Normaaliaika"
"2002-02-02 18:00:00 Normaaliaika"
etc.

Thank you,
Lauri


> sessionInfo()
R version 2.6.0 (2007-10-03)
i386-pc-mingw32

locale:
LC_COLLATE=Finnish_Finland.1252;LC_CTYPE=Finnish_Finland.1252;LC_MONETAR
Y=Finnish_Finland.1252;LC_NUMERIC=C;LC_TIME=Finnish_Finland.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] RODBC_1.2-3

loaded via a namespace (and not attached):
[1] tools_2.6.0

______________________________________________
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