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. ================================Erik-André SAULEAU MD, PhD SEAIM Centre hospitalier 87, Avenue d'Altkirch BP 1070 F-68051 Mulhouse Cédex Tel: (0033)389646753 Mel: sauleauea@ch-mulhouse.fr <mailto:sauleauea@ch-mulhouse.fr> Web: www.ch-mulhouse.fr <http://www.ch-mulhouse.fr/> ================================Registre des Cancers du Haut-Rhin 9, Rue du Dr Mangeney BP 1370 F-68070 Mulhouse Cédex Tel: (0033)389646251 Web: www.arer68.org <http://www.arer68.org/> ================================ ********************************************************************************** Afin d'eviter toute propagation de virus informatique, et en complement des dispositifs en place, ce message (et ses pieces jointes s'il y en a) a ete automatiquement analyse par un antivirus de messagerie. ********************************************************************************** [[alternative HTML version deleted]]
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