[R] Lining up x-y datasets based on values of x

Gabor Grothendieck ggrothendieck at gmail.com
Fri Feb 2 01:24:49 CET 2007


The zoo package has a multiway merge with optional zero fill.
Here are two ways:

library(zoo)
merge(x = zoo(x[,2], x[,1]),
      y = zoo(y[,2], y[,1]),
      z = zoo(z[,2], z[,1]),
      fill = 0)

# or

library(zoo)
X <- list(x = x, y = y, z = z)
merge0 <- function(..., fill = 0) merge(..., fill = fill)
do.call("merge0", lapply(X, function(x) zoo(x[,2], x[,1])))

To get more info on zoo try:

vignette("zoo")

On 2/1/07, Christos Hatzis <christos at nuverabio.com> wrote:
> Hi,
>
> I was wondering if there is a direct approach for lining up 2-column
> matrices according to the values of the first column.  An example and a
> brute-force approach is given below:
>
> x <- cbind(1:10, runif(10))
> y <- cbind(5:14, runif(10))
> z <- cbind((-4):5, runif(10))
>
> xx <- seq( min(c(x[,1],y[,1],z[,1])), max(c(x[,1],y[,1],z[,1])), 1)
> w <- cbind(xx, matrix(rep(0, 3*length(xx)), ncol=3))
>
> w[ xx >= x[1,1] & xx <= x[10,1], 2 ] <- x[,2]
> w[ xx >= y[1,1] & xx <= y[10,1], 3 ] <- y[,2]
> w[ xx >= z[1,1] & xx <= z[10,1], 4 ] <- z[,2]
>
> w
>
> I appreciate any pointers.
>
> Thanks.
>
> Christos Hatzis, Ph.D.
> Nuvera Biosciences, Inc.
> 400 West Cummings Park
> Suite 5350
> Woburn, MA 01801
> Tel: 781-938-3830
> www.nuverabio.com
>
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list