[R] IFELSE across a 3D array?

Sander Oom sander at oomvanlieshout.net
Mon Nov 22 20:59:52 CET 2004


Dear all,

We are trying to clean multiple realizations of a pattern. Erroneous NODATA 
and spurious DATA occur in the realizations. As we have to do a 1000 
realizations for many patterns, efficiency of the code is important.

We need to correct the realizations with a 'mask' pattern of DATA/NODATA. 
We think an ifelse should do the job. Spurious DATA will be simply removed 
using the mask. Erroneous NODATA should be filled in by averaging across 
the cell's nearest neighbors.

The ifelse table is as follows:
IFELSE criteria to determine TARGET from realization and mask
Real  Mask  Target
10     10      10
10      1     0/1 determined through post processing function (average 
across neighbors)
0/1    10      10
0/1     1     0/1

We think that an APPLY on a multi dimensional array is the way to go, with 
each realization (2 dimensions) being a dimension of the array (like a 
stack of maps)

This is where we have got so far:

*********************
library(abind)

#Sim: 0=Absent; 1=Present; 10=NODATA
vectSim <- 
c(0,1,0,1,10,0,1,0,1,1,10,0,1,1,0,1,0,10,10,1,0,1,0,1,0,1,1,10,0,10,10,1,0,1,0,10)
#Mask: 1=DATA; 10=NODATA
vectMask <- c(10,1,10,1,1,10,1,10,1,10,10,1)
length(vectSim)
length(vectMask)

numRow<-3
numCol<-4
numReal<-3

Sim <- array(vectSim, c(numRow,numCol,numReal))
Sim
Mask <- array(vectMask, c(numRow,numCol))
Mask

SmoothSim <- apply(Sim, c(1,2), ifelse(MaskSim==10,33,99))
SmoothSim

********************

Any help is much appreciated!

Thanks,

Sander and Alessandro.




More information about the R-help mailing list