[R] manipulating 3-way array

Luis Ridao Cruz Luisr at frs.fo
Thu Oct 18 14:48:31 CEST 2007


R-Help,

I have a 3 dimensional array with dimensions:

> dim(test)
[1] 241 104  12

I copy-paste an example of the object

> test[1:5,1:5,1:2]
, , ar = 1996

      slagnr
puntar        1        2 3        4  5
   I1  60.30303 36.50610 0  0.00000  0
   I10 58.19512 35.48209 0 75.00000  0
   I11  0.00000  0.00000 0  0.00000  0
   I13 57.47000 33.84501 0 67.33333  0
   I15 57.21971 41.59078 0  0.00000 54

, , ar = 1997

      slagnr
puntar        1        2 3 4  5
   I1  54.32000 42.75032 0 0 51
   I10 59.04167 41.16009 0 0  0
   I11  0.00000  0.00000 0 0  0
   I13 55.18274 40.59340 0 0  0
   I15 58.64486 43.91055 0 0  0


There are many columns which are zero and I wish to get rid of them
and do the following:

for(i in 1:12)
{
ind0     <- apply(test[,,i], 2, sum) != 0 
DIM     <- dim(test[, ind0, i]) 
testU0 <- array(0, dim = DIM)
testU0 <- test[, ind0, i]      
}

but this will override the object testU0 every time the loop is run

Is there a nicer and simpler solution for this?

Thanks in advance


> version
               _                           
platform       i386-pc-mingw32             
arch           i386                        
os             mingw32                     
system         i386, mingw32               
status                                     
major          2                           
minor          6.0                         
year           2007                        
month          10                          
day            03                          
svn rev        43063                       
language       R                           
version.string R version 2.6.0 (2007-10-03)
>



More information about the R-help mailing list