Hi Steven,

Try this:

R> apply(m,1, function(x) ifelse(all(is.na(x)), NA, max(x, na.rm = TRUE)))
[1] NA  8  9

See ?ifelse, ?all and ?max for more information.

HTH,
Jorge


On Sun, 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
>
>        [[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.
>

	[[alternative HTML version deleted]]

