Skip to content
Back to formatted view

Raw Message

Message-ID: <f8fcbc833e3e1.3e3e1f8fcbc83@student.uva.nl>
Date: 2009-03-20T00:25:12Z
From: T.R. Marshall
Subject: Mean-replacing NAs in a 3d array

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