On Apr 18, 2010, at 12:16 AM, steven mosher wrote:

Is there a simple way to calculate the maximum for a row or column of a
matrix when there are NA,s present.

# given a matrix that has any number of NA per row
m<-matrix(c(seq(1,9)),nrow=3)
m
    [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
m[3,1]=NA
m[1,]=NA
m
    [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]    2    5    8
[3,]   NA    6    9

# applying max to rows doesnt work as max returns
# NA if any of the elements is NA.
row_max<-apply(m,1,max)
row_max
[1] NA  8 NA

# my desired result given m would be:
#  NA, 8, 9

Not exactly your desired result, but surely you could fix that:

> row_max<-apply(m,1,max, na.rm=TRUE)
Warning message:
In FUN(newX[, i], ...) : no non-missing arguments to max; returning -Inf
> row_max
[1] -Inf    8    9



        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to