I'm manipulating three date objects (class : "POSIXlt" "POSIXt")
. The two first vectors (start
and end
) define start and end points of some intervals and the third vector (inc
) corresponds to some incidents. What I want to detect is that, which incident has happened in which interval. I reduced the size of my vectors to to provide a working example. Otherwise, the real length of vectors is really large.
start <- c("2007-09-16 18:40:27 GMT","2007-09-28 23:53:55 GMT", "2007-10-25 05:23:01 GMT")
end <- c("2007-09-19 18:40:27 GMT", "2007-10-01 23:53:55 GMT","2007-10-28 05:23:01 GMT")
inc <- c("2007-09-17 18:45:00 GMT", "2007-09-17 19:00:00 GMT", "2007-09-17 19:15:00 GMT", "2007-09-17 19:30:00 GMT")
Here is the simple code to detect the corresponding dates :
quel.eve <- sapply( inc, function(s)
which(start <= s & end >=s) )
When I use ‘which(start <= “2007-09-17 18:45:00 GMT” & end >=2007-09-17 18:45:00 GMT)’ it works properly and returns 1. The problem arises only if i want to apply ‘sapply’. it gives some strange results :
$sec
integer(0)
$min
integer(0)
$hour
integer(0)
$mday
integer(0)
$mon
integer(0)
$year
integer(0)
$wday
integer(0)
$yday
integer(0)
$isdst
integer(0)
In this question I found out that since the ‘POSIXct’ is already a list in its nature, ‘sapply’ cannot deal with it. The elements of vectors that are provided here are copied from my consol and that’s why they resemble to characters. In my program they are definitely ‘Date’ objects.
Is there a way, a part from converting them to POSIXct
, to do so? Your help would be appreciated.