On 6/7/05, SAULEAU Erik-André <[EMAIL PROTECTED]> 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 ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html