Hi, i have two data.frame x and y like : > x <- data.frame( num = c(1:10), value = runif(10) ) > y <- data.frame( num = c(6:10), value = runif(5) ) and i want to obtain something like : num.x value.x num.y value.y 1 0.38423828 NA 0.2911089 2 0.17402507 NA 0.8455208 3 0.54443465 NA 0.8782199 4 0.04540406 NA 0.3202252 5 0.46052426 NA 0.7560559 6 0.61385464 6 0.2911089 7 0.48274968 7 0.8455208 8 0.11961778 8 0.8782199 9 0.64531394 9 0.3202252 10 0.92052805 10 0.7560559 with NA in case of missing value for y to x. { for this example : i write simply > data.frame(num.x=c(1:10), value.x=x[[2]],num.y=c(rep(NA,5),6:10),value.y=y[[2]]) } I didn't find solution in merge(x,y,by="num") : missing rows are no keeping. Can't you help me ? thanks, Bruno Si vous n'etes pas destinataires de ce message, merci d'avertir l'expediteur de l'erreur de distribution et de le detruire immediatement. Ce message contient des informations confidentielles ou appartenant a La Francaise des Jeux. Il est etabli a l'intention exclusive de ses destinataires. Toute divulgation, utilisation, diffusion ou reproduction (totale ou partielle) de ce message ou des informations qu'il contient, doit etre prealablement autorisee. Tout message electronique est susceptible d'alteration et son integrite ne peut etre assuree. La Francaise des Jeux decline toute responsabilite au titre de ce message s'il a ete modifie ou falsifie. If you are not the intended recipient of this e-mail, please notify the sender of the wrong delivery and delete it immediately from your system. This e-mail contains confidential information or information belonging to La Francaise des Jeux and is intended solely for the addressees. The unauthorised disclosure, use, dissemination or copying (either whole or partial) of this e-mail, or any information it contains, is prohibited. E-mails are susceptible to alteration and their integrity cannot be guaranteed. La Francaise des Jeux shall not be liable for this e-mail if modified or falsified.
> x <- data.frame( 1:10, runif(10) ) > y <- data.frame( 6:10, runif(5) ) > merge(x, y, by=1, all=TRUE)X1.10 runif.10. runif.5. 1 1 0.4915303 NA 2 2 0.7108826 NA 3 3 0.5658456 NA 4 4 0.4201561 NA 5 5 0.9575464 NA 6 6 0.1650210 0.82674151 7 7 0.2198187 0.24383035 8 8 0.4571945 0.01177674 9 9 0.9026321 0.76861464 10 10 0.1046505 0.23990883 On Fri, 2004-07-23 at 16:07, Bruno Cutayar wrote:> Hi, > i have two data.frame x and y like : > > x <- data.frame( num = c(1:10), value = runif(10) ) > > y <- data.frame( num = c(6:10), value = runif(5) ) > and i want to obtain something like : > > num.x value.x num.y value.y > 1 0.38423828 NA 0.2911089 > 2 0.17402507 NA 0.8455208 > 3 0.54443465 NA 0.8782199 > 4 0.04540406 NA 0.3202252 > 5 0.46052426 NA 0.7560559 > 6 0.61385464 6 0.2911089 > 7 0.48274968 7 0.8455208 > 8 0.11961778 8 0.8782199 > 9 0.64531394 9 0.3202252 > 10 0.92052805 10 0.7560559 > > with NA in case of missing value for y to x. > > { for this example : i write simply > > data.frame(num.x=c(1:10), > value.x=x[[2]],num.y=c(rep(NA,5),6:10),value.y=y[[2]]) } > > I didn't find solution in merge(x,y,by="num") : missing rows are no keeping. > Can't you help me ? > > thanks, > Bruno > > > > Si vous n'etes pas destinataires de ce message, merci d'avertir l'expediteur de l'erreur de distribution et de le detruire immediatement. > Ce message contient des informations confidentielles ou appartenant a La Francaise des Jeux. Il est etabli a l'intention exclusive de ses destinataires. Toute divulgation, utilisation, diffusion ou reproduction (totale ou partielle) de ce message ou des informations qu'il contient, doit etre prealablement autorisee. > Tout message electronique est susceptible d'alteration et son integrite ne peut etre assuree. La Francaise des Jeux decline toute responsabilite au titre de ce message s'il a ete modifie ou falsifie. > > If you are not the intended recipient of this e-mail, please notify the sender of the wrong delivery and delete it immediately from your system. > This e-mail contains confidential information or information belonging to La Francaise des Jeux and is intended solely for the addressees. The unauthorised disclosure, use, dissemination or copying (either whole or partial) of this e-mail, or any information it contains, is prohibited. > E-mails are susceptible to alteration and their integrity cannot be guaranteed. La Francaise des Jeux shall not be liable for this e-mail if modified or falsified. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Bruno Cutayar wrote:> > > Hi, > i have two data.frame x and y like : > > x <- data.frame( num = c(1:10), value = runif(10) ) > > y <- data.frame( num = c(6:10), value = runif(5) ) > and i want to obtain something like : > > num.x value.x num.y value.y > 1 0.38423828 NA 0.2911089 > 2 0.17402507 NA 0.8455208 > 3 0.54443465 NA 0.8782199 > 4 0.04540406 NA 0.3202252 > 5 0.46052426 NA 0.7560559 > 6 0.61385464 6 0.2911089 > 7 0.48274968 7 0.8455208 > 8 0.11961778 8 0.8782199 > 9 0.64531394 9 0.3202252 > 10 0.92052805 10 0.7560559 > > with NA in case of missing value for y to x. > > { for this example : i write simply > > data.frame(num.x=c(1:10), > value.x=x[[2]],num.y=c(rep(NA,5),6:10),value.y=y[[2]]) } > > I didn't find solution in merge(x,y,by="num") : missing rows are no > keeping. > Can't you help me ? >See ?merge which will tell you about the `all' argument. I believe you want something like (though I'm not completely sure): m <- merge(x, y, by = "num", all = TRUE) # now add `num.x' and `num.y' as in your example na.x <- is.na(m$value.x) na.y <- is.na(m$value.y) m$num.x <- ifelse(m$num %in% x$num, m$num, NA) m$num.y <- ifelse(m$num %in% y$num, m$num, NA) # fill in `value.x' and `value.y' with repeated values m$value.x[na.x] <- rep(x$value, length.out = sum(na.x)) m$value.y[na.y] <- rep(y$value, length.out = sum(na.y)) HTH, --sundar
On Fri, 2004-07-23 at 10:07, Bruno Cutayar wrote:> Hi, > i have two data.frame x and y like : > > x <- data.frame( num = c(1:10), value = runif(10) ) > > y <- data.frame( num = c(6:10), value = runif(5) ) > and i want to obtain something like : > > num.x value.x num.y value.y > 1 0.38423828 NA 0.2911089 > 2 0.17402507 NA 0.8455208 > 3 0.54443465 NA 0.8782199 > 4 0.04540406 NA 0.3202252 > 5 0.46052426 NA 0.7560559 > 6 0.61385464 6 0.2911089 > 7 0.48274968 7 0.8455208 > 8 0.11961778 8 0.8782199 > 9 0.64531394 9 0.3202252 > 10 0.92052805 10 0.7560559 > > with NA in case of missing value for y to x. > > { for this example : i write simply > > data.frame(num.x=c(1:10), > value.x=x[[2]],num.y=c(rep(NA,5),6:10),value.y=y[[2]]) } > > I didn't find solution in merge(x,y,by="num") : missing rows are no keeping. > Can't you help me ? > > thanks, > BrunoThe use of merge(), with the argument 'all' set to TRUE, will get you the following (note my values are different due to not using the same 'seed' value for runif() ):> merge(x, y, by = "num", all = TRUE)num value.x value.y 1 1 0.14057955 NA 2 2 0.60850644 NA 3 3 0.63410731 NA 4 4 0.07196253 NA 5 5 0.51869503 NA 6 6 0.57042428 0.3340535 7 7 0.85874426 0.9340489 8 8 0.03608417 0.5417780 9 9 0.24422205 0.2214993 10 10 0.03383263 0.4947865 The use of 'all = TRUE' will fill in non-matching rows. The default is FALSE. Note here however, that the value.y column is not replicated for the first five rows, as you have above. If that is what you want, you could do something like the following:> cbind(x, y$value)num value y$value 1 1 0.14057955 0.3340535 2 2 0.60850644 0.9340489 3 3 0.63410731 0.5417780 4 4 0.07196253 0.2214993 5 5 0.51869503 0.4947865 6 6 0.57042428 0.3340535 7 7 0.85874426 0.9340489 8 8 0.03608417 0.5417780 9 9 0.24422205 0.2214993 10 10 0.03383263 0.4947865 which takes advantage of the recycling of y$value, since it is shorter than the number of rows in 'x'. In this case, y$value is repeated twice. HTH, Marc Schwartz