khazaei at ceremade.dauphine.fr
2010-Mar-05 12:30 UTC
[R] question on maximum of each row of the matrix
Hi, I used the apply()function to compute the maximum of the each row of the matrix, but in my application sometime the matrix has just one row, and the apply() dosen't work. Could you please give me some hints? thank you khazaei
Hi: Turn it into a matrix:> x <- 1:10 > x <- matrix(x, nrow = 1) > class(x)[1] "matrix"> apply(x, 1, max)[1] 10 HTH, Dennis On Fri, Mar 5, 2010 at 4:30 AM, <khazaei@ceremade.dauphine.fr> wrote:> Hi, > I used the apply()function to compute the maximum of the each row of > the matrix, but in my application sometime the matrix has just one row, > and the apply() dosen't work. > Could you please give me some hints? > > thank you > khazaei > > ______________________________________________ > 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]]
On Mar 5, 2010, at 7:30 AM, khazaei at ceremade.dauphine.fr wrote:> Hi, > I used the apply()function to compute the maximum of the each row > of > the matrix, but in my application sometime the matrix has just one > row, > and the apply() dosen't work. > Could you please give me some hints?If your matrix became a one row entity as a result of an indexing or subsetting operation, then you could prevent the loss of its matrix classification by using the drop=FALSE argument. > strt <- matrix(1:8, ncol=2) > strt[ , 2, drop=FALSE] [,1] [1,] 5 [2,] 6 [3,] 7 [4,] 8 > apply(strt[ , 2, drop=FALSE], 1, max) [1] 5 6 7 8> > thank you > khazaei-- David Winsemius, MD Heritage Laboratories West Hartford, CT