[R] calculate column means when missing data is present

Luke Neraas lukasneraas.r at gmail.com
Tue Nov 27 01:59:27 CET 2007


# Hello,

# I have a matrix that contains some missing values denoted by NA.
# I would like to calculate the mean for each column and not have the
# NA values included.  here is a sample matrix  "X" below.


c1 <- c(1,4,3,2,NA,2,5,6,4,2)
c2 <- c(1,3,6,NA,NA,2,9,6,1,2)
c3 <- c(2,4,2,NA,4,2,NA,6,3,5)
c4 <- c(2,6,4,NA,4,2,NA,6,1,5)
c5 <- c(2,3,2,NA,4,2,9,6,NA,5)

X<-cbind(c1,c2,c3,c4,c5)

X

# I have tried a variety of methods to calculate the Column means but each time
# i do not get the result i am looking for

result<-colMeans(X)  # only returns a mean of NA


X <- X[!is.na(X)]    # Effectively removes the NA values but now I
cannot calculate Column Means

#  Below is the result of the column means I am looking for

result
       c1   c2    c3   c4    c5
 [1,] 3.22 3.75  3.5  3.75 4.125


# any help or ideas would be greatly appreciated

# thanks in advance

Luke Neraas

lukasneraas.r at gmail.com

University of Alaska Fairbanks
School of Fisheries and Ocean Sciences
11120 Glacier Highway
UAF Fisheries Division
Juneau, AK 99801



More information about the R-help mailing list