[R] time segments intersection

[Ricardo Rodriguez] Your XEN ICT Team webmaster at xen.net
Fri Oct 3 09:25:23 CEST 2008


Thanks Mark,

markleeds at verizon.net wrote:
>
>
> # MAKE POSIXct OBJECTS FROM CHARACTER STRINGS
> temp1 <- as.POSIXct(strptime("2007-02-02","%Y-%m-%d"))
> temp2 <- as.POSIXct(strptime("2007-02-01","%Y-%m-%d"))
>
> # TEMP1 AND TEMP2 ARE SORT OF NUMBERS BUT EACH UNIT IS ONE
> # SECOND
>
> result <- difftime(temp1,temp2)
>
> # STILL NOT REALLY A NUMBER
> print(result)
> print(str(result))
>
> # MAKE IT A NUMBER
> numberresult <- unclass(result)
> attributes(numberresult) <- NULL
> print(numberresult)

This does the trick:

endPeriod<-as.POSIXct(strptime("2008-09-30","%Y-%m-%d"))
startPeriod<-as.POSIXct(strptime("2007-10-01","%Y-%m-%d"))
endProject<-as.POSIXct(strptime("2007-12-31","%Y-%m-%d"))
startProject<-as.POSIXct(strptime("2006-12-01","%Y-%m-%d"))

attributes(endPeriod) <- NULL
attributes(startPeriod) <- NULL
attributes(endProject) <- NULL
attributes(startProject) <- NULL

overlappingDays <- 
length(intersect(startPeriod:endPeriod,startProject:endProject))/86400
print(overlappingDays)

[1] 91.04168

But as far as I see here, the method is extremely slow (some seconds for 
a single case). It solves the problem now, but taking into account that 
I must use the method for a quite large database, I think I must look 
for anything faster.

Any new idea will be welcome!!!

Cheers,

Ricardo

-- 
Ricardo Rodríguez
Your XEN ICT Team



More information about the R-help mailing list