[R] Taking the min of each row in a matrix

Will Glass-Husain wglasshusain at gmail.com
Thu Jan 29 17:58:50 CET 2009


Hi,

I'm a new user. I've been reading through the manual and looking at
various examples but am still trying to make sense of the most
efficient ways to handle matrices of data.

If I have a 2D matrix of data, how do I get the mean, min, max value
of each row?  I see the "mean" function on a matrix will give me
averages by row, but min and max give me the value for the entire
matrix.  I want the min (for example) of each row.  pmin looks useful,
but I can't seem to get the syntax right to apply to each column.

Right now I am doing this.  Is there a one-liner that would work instead?

minResult <- vector (mode="list", length=rowCount)
for (row in 1:rowCount)
{
	minResult[[row]] <- min(corResult[row], na.rm = TRUE)			
}

Thanks in advance.

WILL




More information about the R-help mailing list