Re: [R] minimum of each row in a matrix

From: Sarah Goslee <sarah.goslee_at_gmail.com>
Date: Tue, 08 May 2007 09:23:35 -0400

Check out the help for apply, particularly the MARGIN argument:

minOfRows=apply(a, 1, function(x) min(x[x!=0]) ) maxOfRows=apply(a, 1, function(x) max(x) )

Sarah

On 5/8/07, Schmitt, Corinna <Corinna.Schmitt_at_igb.fraunhofer.de> wrote:
> Hallo,
>
> I just followed the discussion of the title: minimum from matrix.
> I have a similar problem.
>
> > a=matrix(c(0,2, 0, 0, 1, 3, 0, 3, 2, 0, 3, 5, 0, 4, 0, 0),ncol=4)
> > a=rbind(a,1:4)
> > a
> [,1] [,2] [,3] [,4]
> [1,] 0 1 2 0
> [2,] 2 3 0 4
> [3,] 0 0 3 0
> [4,] 0 3 5 0
> [5,] 1 2 3 4
> >
> > minOfColumns=apply(a, 2, function(x) min(x[x!=0]) )
> > minOfColumns
> [1] 1 1 2 4
> > maxOfColumns=apply(a, 2, function(x) max(x) )
> > maxOfColumns
> [1] 2 3 5 4
> >
>
> How looks like the command for the minimum of the rows? I tried several
> possibilities with apply but did not get the wanted result. The result
> should be minOfRows = 0 0 0 0 1
>
> Thanks,
> Corinna
>

-- 
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
R-help_at_stat.math.ethz.ch 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.
Received on Tue 08 May 2007 - 13:32:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 08 May 2007 - 14:31:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.