[R] Variables values on intersected intervals

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jun 8 03:28:09 CEST 2005


On 6/7/05, SAULEAU Erik-André <SAULEAUEA at ch-mulhouse.fr> wrote:
> Dear R-list,
> 
> i have a problem, in the framework of simulations, i want to vectorize for
> earning time: a variable, say X, has values on intervals and an other
> variable, say Y, has values on other intervals. For example
> 
> Inf     Sup     X
> 0     2     1
> 2     4    2
> 4     6    3
> 
> and
> 
> Inf     Sup     Y
> 1     3     1
> 3     5    2
> 5     7    3
> 
> i want to create a matrix like this
> 
> Inf     Sup     X     Y
> 0     1       1     NA
> 1     2      1     1
> 2     3      2     1
> 3     4      2     2
> 4     5      3     2
> 5     6      3     3
> 6     7      NA     3
> 
> I get it with loops but it seems to me that it can take a lot of time for a
> large number of intervals. I cannot get it with some matrix or vector
> manipulation (quicker than loops). Any solution???
> 
> thank you in advance, with my best regards, erik.
> 

Suppose dx and dy are the input data frames.  The 
intervals of dz, the output data frame, are made up of the
union of boundaries, b, of dx and dy.

Then we use cut to look up each dz interval
in each of dx and dy.

b <- sort(union(unlist(dx[,-3]), unlist(dy[,-3])))
dz <- data.frame(Inf. = b[-length(b)], Sup = b[-1])

kut <- function(g, df) 
   cut(g, c(df$Inf., max(df$Sup)), right = FALSE, lab = FALSE)

dz$X <- dx[kut(dz$Inf., dx), "X"]
dz$Y <- dy[kut(dz$Inf., dy), "Y"]

dz




More information about the R-help mailing list