[R] time segments intersection

[Ricardo Rodriguez] Your XEN ICT Team webmaster at xen.net
Sun Oct 5 00:13:18 CEST 2008


Gabor Grothendieck wrote:
>
> Mark did not post his response so I don't know what it
> is.   

This is Mark's proposal. Sorry, I was speaking about it as if posted to 
the list.

# 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)

> How you do it may depend on your setup which was
> not entirely clear from the question since it started out
> as if the two series were the inputs and then seemed to be
> assuming the time ranges were.
>   

Ranges are the case study. I have a table with dates like these:

startProject    endProject
2006/12/01    2007/05/31
2007/02/01    2008/12/31
2007/02/01    2008/12/31
2007/02/01    2008/12/31
2007/02/01    2008/12/31
2006/11/22    2009/12/30
2003/01/01    2006/07/31
2004/12/28    2007/12/27
2005/12/23    2006/12/30
2008/01/01    2010/12/31
2005/12/23    2008/12/30
2005/12/23    2008/12/30

For each record, I  need the value for a new variable (let's call it 
timeInCommon) that stores a numeric value: the number days that each 
range startProject:endProject intersects with a third range, let's call 
it t1:t2.
> Here are a few possibilities assuming z1 and z2
> from my prior post.  Depending on what you want
> you may need to add 1.
>
> diff(range(time(merge(z1, z2, all = FALSE))))
>
> diff(range(intersect(time(z1), time(z2))))
>
> r1 <- range(time(z1))
> r2 <- range(time(z2))
> pmin(r1, r2)[2] - pmax(r1, r2)[1]
>
> The first one generalizes to N series immediately.  The
> second does not but is slightly shorter (although abbreviation
> of the first could get that one even shorter).  The last
> reduces the series to time ranges and then operates on
> those.
>
>   
The second option generates directly the required numeric value. 5 days 
if we use the time series coming with your first message. My problem now 
is how to use it to populate this new timeInCommon variable for all and 
each records.

Thanks for your help,

Ricardo


-- 
Ricardo Rodríguez
Your XEN ICT Team



More information about the R-help mailing list