Skip to content
Prev 174324 / 398506 Next

Mean-replacing NAs in a 3d array

Hi Tom,
it would have been nice (and it is in fact requested by the posting 
guide) to give a running example instead of letting us construct your data.
Anyway, it wasn't too hard with
 Xa<-array(1:12,dim=c(2,2,3))
 Xa[c(2,6,8)]<-NA

#so next, create a vector for the colMeans with length 2*2*3, repeating 
each mean 4 times
cM<-rep(colMeans(Xa,na.rm=TRUE,dims=2),each=dim(Xa)[1]*dim(Xa)[2])

#... and use the fact, that most r-objects have also an one-dimensional 
index (which was used in the construction above as well)
Xa[is.na(Xa)]<-cM[is.na(Xa)]

hth.

T.R. Marshall schrieb: