Skip to content

alternate storage options

2 messages · dxc13, Moshe Olshansky

#
useR's.

I am working with an algorithm in which I will need to create combinations
of all the points I have in a matrix.  When I have 2 variables, I simply use
expand.grid() to do this.  Sometimes I am faced with 3 or more variables and
if I run expand.grid(), R cannot process it due to the huge size.  Is there
any efficient way to workaround this? 

Thanks,
Derek
#
You can use a loop...

If x,y and z are your vectors containing Nx,Ny and Nz
numbers respectively, then
for (Ix in 1:Nx) for (Iy in 1:Ny) for (Iz in 1:Nz) {
Point <- c(x[Ix],y[Iy],z[Iz])
do whatever you need with Point
}

A (probably better) compromise may be:

a <- matrix(0,nrow = Ny*Nz, ncol = 3)
for (i in 1:Nx) {
a[,2:3] <- expand.grid(y,z)
a[,1] <- x[i]
do whatever you want with a
}
--- dxc13 <dxc13 at health.state.ny.us> wrote:

            
http://www.nabble.com/alternate-storage-options-tp14438736p14438736.html