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

jim holtman jholtman at gmail.com
Thu Jan 29 18:24:28 CET 2009


?apply

e.g. for the 'min'

apply(yourMatrix, 1, min, na.rm=TRUE)

On Thu, Jan 29, 2009 at 11:58 AM, Will Glass-Husain
<wglasshusain at gmail.com> wrote:
> 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
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list