[R] merge, cbind, or....?
Sundar Dorai-Raj
sundar.dorai-raj at PDF.COM
Fri Jul 23 17:27:55 CEST 2004
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
More information about the R-help
mailing list