[R] minimum of each row in a matrix

2007-05-08 Thread Schmitt, Corinna
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,]0120
[2,]2304
[3,]0030
[4,]0350
[5,]1234

 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

__
R-help@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.


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

2007-05-08 Thread Sarah Goslee
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 [EMAIL PROTECTED] 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,]0120
 [2,]2304
 [3,]0030
 [4,]0350
 [5,]1234
 
  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@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.


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

2007-05-08 Thread Vladimir Eremeev

Looks like you are reading manuals and these mailings insufficiently
carefully.

?apply says that if its second argument is 1, it gives you what you want.

Gabor Csardi has also written you this.

If you have several vectors, not a single matrix, you can use pmin:
pmin(a[1,],a[2,],a[3,],a[4,],a[5,])
However, this variant is inefficient in this particular case (lots of
typing, and hardcoded).


Schmitt, Corinna 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,]0120
 [2,]2304
 [3,]0030
 [4,]0350
 [5,]1234

 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
 
 

-- 
View this message in context: 
http://www.nabble.com/minimum-of-each-row-in-a-matrix-tf3709616.html#a10375966
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


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

2007-05-08 Thread Gabor Csardi
Corinna, 

what about reading my previous mail, which you suggested to forget?

Gabor

ps. it is apply(a, 1, min)

On Tue, May 08, 2007 at 03:21:26PM +0200, Schmitt, Corinna 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,]0120
 [2,]2304
 [3,]0030
 [4,]0350
 [5,]1234
 
  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
 
 __
 R-help@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.

-- 
Csardi Gabor [EMAIL PROTECTED]MTA RMKI, ELTE TTK

__
R-help@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.


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

2007-05-08 Thread Schmitt, Corinna
Hi Peter,

thanks for the quick help. Yes it does what I want.

Thanks, Corinna



  



Von: Peter Konings [mailto:[EMAIL PROTECTED] 
Gesendet: Dienstag, 8. Mai 2007 15:29
An: Schmitt, Corinna
Betreff: Re: [R] minimum of each row in a matrix



Hi Corinna,

does
apply(a, 1, min)
do what you want?

HTH
Peter.

On 5/8/07, Schmitt, Corinna  [EMAIL PROTECTED]
mailto:[EMAIL PROTECTED]  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,]0120
[2,]2304
[3,]0030
[4,]0350
[5,]1234

 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

__
R-help@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. 




[[alternative HTML version deleted]]

__
R-help@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.