hi all are we able to combine column vectors of different lengths such that the result appears in matrix form? e.g. a=1 b=1:3 d=1:4 then z=CBIND(a,b,d) 1 1 1 2 2 3 3 4 i stil want the following! z[,1]=1 z[,2]=1:3 z[,3]=1:5 i made up the name of this function. we could use "cbind" but it does not seem to allows this! thanking you in advance. / allan
Clark Allan wrote:> hi all > > > are we able to combine column vectors of different lengths such that the > result appears in matrix form? > > e.g. > > a=1 > b=1:3 > d=1:4 > > then > > z=CBIND(a,b,d) > > > 1 1 1 > 2 2 > 3 3 > 4 > > i stil want the following! > z[,1]=1 > z[,2]=1:3 > z[,3]=1:5 > > i made up the name of this function. we could use "cbind" but it does > not seem to allows this!See my other message: You probably want a list. If not, the sparce matrix classes provided by package Matrix might be worth considering. Uwe Ligges> thanking you in advance. > > / > allan > > > ------------------------------------------------------------------------ > > ______________________________________________ > 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
Clark Allan wrote:> hi all > > > are we able to combine column vectors of different lengths such that the > result appears in matrix form? > > e.g. > > a=1 > b=1:3 > d=1:4 > > then > > z=CBIND(a,b,d) > > > 1 1 1 > 2 2 > 3 3 > 4 > > i stil want the following! > z[,1]=1 > z[,2]=1:3 > z[,3]=1:5 > > i made up the name of this function. we could use "cbind" but it does > not seem to allows this! > > thanking you in advance. > > / > allanHi, Allan, How about the following: cbind.all <- function(..., fill.with = NA) { args <- list(...) len <- sapply(args, NROW) if(diff(rng <- range(len)) > 0) { maxlen <- rng[2] pad <- function(x, n) c(x, rep(fill.with, n)) for(j in seq(along = args)) { if(maxlen == len[j]) next if(is.data.frame(args[[j]])) { args[[j]] <- lapply(args[[j]], pad, maxlen - len[j]) args[[j]] <- as.data.frame(args[[j]]) } else if(is.matrix(args[[j]])) { args[[j]] <- apply(args[[j]], 2, pad, maxlen - len[j]) } else if(is.vector(args[[j]])) { args[[j]] <- pad(args[[j]], maxlen - len[j]) } else { stop("... must only contain data.frames or arrays.") } } } do.call("cbind", args) } cbind.all(data.frame(a=1), data.frame(a=c(2,1)), x = 1, y = matrix(1:4,2,2)) cbind.all(a = 1, b = 1:3, d = 1:4) HTH, --sundar
On 8/8/05, Clark Allan <Allan at stats.uct.ac.za> wrote:> hi all > > > are we able to combine column vectors of different lengths such that the > result appears in matrix form? > > e.g. > > a=1 > b=1:3 > d=1:4 > > then > > z=CBIND(a,b,d) > > > 1 1 1 > 2 2 > 3 3 > 4 > > i stil want the following! > z[,1]=1 > z[,2]=1:3 > z[,3]=1:5 > > i made up the name of this function. we could use "cbind" but it does > not seem to allows this!There are a number of alternatives: # 1. just create a list x1 <- list(a = 1, b = 1:3, c = 1:4) # 2. create a ts object: x2 <- do.call("cbind", lapply(x1, ts)) # 3. create a matrix from the ts object x3 <- unclass(do.call("cbind", lapply(d, ts))) tsp(x3) <- NULL
On 8/8/05, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On 8/8/05, Clark Allan <Allan at stats.uct.ac.za> wrote: > > hi all > > > > > > are we able to combine column vectors of different lengths such that the > > result appears in matrix form? > > > > e.g. > > > > a=1 > > b=1:3 > > d=1:4 > > > > then > > > > z=CBIND(a,b,d) > > > > > > 1 1 1 > > 2 2 > > 3 3 > > 4 > > > > i stil want the following! > > z[,1]=1 > > z[,2]=1:3 > > z[,3]=1:5 > > > > i made up the name of this function. we could use "cbind" but it does > > not seem to allows this! > > There are a number of alternatives: > > # 1. just create a list > > x1 <- list(a = 1, b = 1:3, c = 1:4) > > # 2. create a ts object: > > x2 <- do.call("cbind", lapply(x1, ts)) > > # 3. create a matrix from the ts object > > x3 <- unclass(do.call("cbind", lapply(d, ts))) > tsp(x3) <- NULL >That last one should have been: x3 <- unclass(x2) tsp(x3) <- NULL