[R] cbind()

Duncan Murdoch murdoch at stats.uwo.ca
Wed Oct 31 13:22:05 CET 2007


On 10/31/2007 8:06 AM, livia wrote:
> Hello,
> 
> I would like to use the cbind() function to construct a matrix used in the
> middle of a function as following
> 
> for (i in 1:1000) {
> b[i] <- function(cbind(a[[1]][[i]],
> a[[2]][[i]],a[[3]][[i]],...a[[67]][[i]]))
> }
> 
> 
> Is there an easy way of achieving this rather than "cbind" every column? 

What you're doing is a little complicated (taking the i'th element out 
of elements 1:67 in a list), so the solution is bound to be a little 
complicated too.  But something like this will save you typing:

# construct a list of the i'th elements
ithelements <- lapply(a[1:67], function(e) e[[i]])

# pass them as args to cbind
do.call(cbind, ithelements)

I haven't tested this (not having your data at hand), but if it doesn't 
work, something like it should.

Duncan Murdoch



More information about the R-help mailing list