How to avoid a for() loop: Coercing a 2 dimensional array into a 6 dimensional array

Tag: arrays , r Author: wanghuancxs Date: 2014-02-03

I'm not sure exactly how to title this question but here goes:

I am writing a program where I have the object "eff" which is a 2 dimensional array (eff[ar,fl) where ar is area and fl is fleet. Currently there are 3 areas and 2 fleets.

In order to run the simulator I need to force these 2 dimensions into an object called "effort" which is a 6 dimensional array (effort[,yr,,,ar,fl]). I want the area and fleet dimensions from eff to be copied into the corresponding dimensions of effort.

Currently I am using a for() loop:

    eff<-array(init.eff, dim=c(NAREAS,NFLEETS))
       for( fl in flt )
       for( ar in area) effort[,yr,,,ar,fl] <<- eff[ar,fl]

I can't seem to find out how to do this without using the for() loop. It seems like this should be an elementary operation in R programming but I have been struggling.


You should be able to just do a single assignment using the right dimension subscripts. Without a reproducible example, I hesitate to guess at the details though.