[R] Mean-replacing NAs in a 3d array

T.R. Marshall T.Marshall at student.uva.nl
Fri Mar 20 01:25:12 CET 2009


Hi all

I have a 3d array containing missing values.

> Xa
, , 1

     [,1] [,2]
[1,]    1    3
[2,]   NA    4

, , 2

     [,1] [,2]
[1,]    5    7
[2,]   NA   NA

, , 3

     [,1] [,2]
[1,]    9   11
[2,]   10   12

I want to replace the missing values with the mean, but the mean of each
'page' in the array (wrong terminology I'm sure). So - for the array
above - [2,1,2] and [2,2,2] should both read '6', and [2,1,1] should be
'2.66667'.

I can obtain a vector of those means by:
> colMeans(Xa,na.rm=TRUE,dims=2)
[1]  2.666667  6.000000 10.500000

But what to do next?

Help would be appreciated. Also I'm a noob to this mailing list so
apologies if I've not given enough detail or failed to find the answer
somewhere obvious.

Best
Tom Marshall, Universiteit van Amsterdam




More information about the R-help mailing list