steven mosher
2010-Apr-18 04:16 UTC
[R] Calculating a Maximum for a row or column with NA's
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]]
David Winsemius
2010-Apr-18 05:01 UTC
[R] Calculating a Maximum for a row or column with NA's
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, 9Not 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 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.David Winsemius, MD West Hartford, CT
Jorge Ivan Velez
2010-Apr-18 05:31 UTC
[R] Calculating a Maximum for a row or column with NA's
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]]