[R] calculate column means when missing data is present
Peter Alspach
PAlspach at hortresearch.co.nz
Tue Nov 27 02:27:18 CET 2007
Luke
Check
?colMeans or help(colMeans)
you will see there is an agrument, na.rm, which by default is FALSE -
you need to set it to TRUE.
Peter Alspach
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Luke Neraas
> Sent: Tuesday, 27 November 2007 1:59 p.m.
> To: r-help
> Subject: [R] calculate column means when missing data is present
>
> # 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
The contents of this e-mail are privileged and/or confidential to the named
recipient and are not to be used by any other person and/or organisation.
If you have received this e-mail in error, please notify the sender and delete
all material pertaining to this e-mail.
More information about the R-help
mailing list