I have a very large matrix with columns that have some of their entries as zero A small example if a [,1] [,2] [,3] [,4] [,1] 0 2 0 0 [,2] 1 3 0 3 [,3] 2 0 3 5 [,4] 0 4 0 0 and what to get the minimum number from each column but that number should not be zero. If I use apply (a,2,min) I will get a vector of zeros as the minimum but what I want it for example from column 1 I should get 1 i.e for all the matrix I should get a vector (1,2,3,3). I wonder if someone can give an idea on how to go about it. thanks in advance for your help. Oarabile
apply(a, 2, function(x) min(x[x!=0]) ) should do it. Might need some improvement if all numbers in a column can be zero, try it. Gabor On Tue, May 08, 2007 at 09:50:43AM +0100, oarabile at stams.strath.ac.uk wrote:> > > I have a very large matrix with columns that have some of their > entries as zero > > > A small example if a> > [,1] [,2] [,3] [,4] > [,1] 0 2 0 0 > [,2] 1 3 0 3 > [,3] 2 0 3 5 > [,4] 0 4 0 0 > > and what to get the minimum number from each column but that number > should not be zero. If I use apply (a,2,min) I will get a vector of > zeros as the minimum but what I want it for example from column 1 I > should get 1 i.e for all the matrix I should get a vector (1,2,3,3). I > wonder if someone can give an idea on how to go about it. > > thanks in advance for your help. > > Oarabile > > ______________________________________________ > 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.-- Csardi Gabor <csardi at rmki.kfki.hu> MTA RMKI, ELTE TTK
try this: apply(a, 2, function(x) min(x[x > 0])) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: <oarabile at stams.strath.ac.uk> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, May 08, 2007 10:50 AM Subject: [R] minimum from matrix> > > I have a very large matrix with columns that have some of their > entries as zero > > > A small example if a> > [,1] [,2] [,3] [,4] > [,1] 0 2 0 0 > [,2] 1 3 0 3 > [,3] 2 0 3 5 > [,4] 0 4 0 0 > > and what to get the minimum number from each column but that number > should not be zero. If I use apply (a,2,min) I will get a vector of > zeros as the minimum but what I want it for example from column 1 I > should get 1 i.e for all the matrix I should get a vector (1,2,3,3). > I > wonder if someone can give an idea on how to go about it. > > thanks in advance for your help. > > Oarabile > > ______________________________________________ > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Hallo, I added one row:> 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 And how looks like the command for the minimum of the rows? The result should be minOfRows = 0 0 0 0 1 Thanks, Corinna -----Urspr?ngliche Nachricht----- Von: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] Im Auftrag von Gabor Csardi Gesendet: Dienstag, 8. Mai 2007 14:03 An: oarabile at stams.strath.ac.uk Cc: r-help at stat.math.ethz.ch Betreff: Re: [R] minimum from matrix apply(a, 2, function(x) min(x[x!=0]) ) should do it. Might need some improvement if all numbers in a column can be zero, try it. Gabor On Tue, May 08, 2007 at 09:50:43AM +0100, oarabile at stams.strath.ac.uk wrote:> > > I have a very large matrix with columns that have some of their > entries as zero > > > A small example if a> > [,1] [,2] [,3] [,4] > [,1] 0 2 0 0 > [,2] 1 3 0 3 > [,3] 2 0 3 5 > [,4] 0 4 0 0 > > and what to get the minimum number from each column but that number > should not be zero. If I use apply (a,2,min) I will get a vector of > zeros as the minimum but what I want it for example from column 1 I > should get 1 i.e for all the matrix I should get a vector (1,2,3,3). I > wonder if someone can give an idea on how to go about it. > > thanks in advance for your help. > > Oarabile > > ______________________________________________ > 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.-- Csardi Gabor <csardi at rmki.kfki.hu> MTA RMKI, ELTE TTK ______________________________________________ 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.
Thanks it worked. Oarabile Gabor Csardi wrote:>apply(a, 2, function(x) min(x[x!=0]) ) > >should do it. Might need some improvement if all numbers in a column >can be zero, try it. > >Gabor > >On Tue, May 08, 2007 at 09:50:43AM +0100, oarabile at stams.strath.ac.uk wrote: > > >>I have a very large matrix with columns that have some of their >>entries as zero >> >> >>A small example if a>> >> [,1] [,2] [,3] [,4] >> [,1] 0 2 0 0 >> [,2] 1 3 0 3 >> [,3] 2 0 3 5 >> [,4] 0 4 0 0 >> >>and what to get the minimum number from each column but that number >>should not be zero. If I use apply (a,2,min) I will get a vector of >>zeros as the minimum but what I want it for example from column 1 I >>should get 1 i.e for all the matrix I should get a vector (1,2,3,3). I >>wonder if someone can give an idea on how to go about it. >> >>thanks in advance for your help. >> >>Oarabile >> >>______________________________________________ >>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. >> >> > > >
Thanks it worked Oarabile Dimitris Rizopoulos wrote:> try this: > > apply(a, 2, function(x) min(x[x > 0])) > > > I hope it helps. > > Best, > Dimitris > > ---- > Dimitris Rizopoulos > Ph.D. Student > Biostatistical Centre > School of Public Health > Catholic University of Leuven > > Address: Kapucijnenvoer 35, Leuven, Belgium > Tel: +32/(0)16/336899 > Fax: +32/(0)16/337015 > Web: http://med.kuleuven.be/biostat/ > http://www.student.kuleuven.be/~m0390867/dimitris.htm > > > ----- Original Message ----- From: <oarabile at stams.strath.ac.uk> > To: <r-help at stat.math.ethz.ch> > Sent: Tuesday, May 08, 2007 10:50 AM > Subject: [R] minimum from matrix > > >> >> >> I have a very large matrix with columns that have some of their >> entries as zero >> >> >> A small example if a>> >> [,1] [,2] [,3] [,4] >> [,1] 0 2 0 0 >> [,2] 1 3 0 3 >> [,3] 2 0 3 5 >> [,4] 0 4 0 0 >> >> and what to get the minimum number from each column but that number >> should not be zero. If I use apply (a,2,min) I will get a vector of >> zeros as the minimum but what I want it for example from column 1 I >> should get 1 i.e for all the matrix I should get a vector (1,2,3,3). I >> wonder if someone can give an idea on how to go about it. >> >> thanks in advance for your help. >> >> Oarabile >> >> ______________________________________________ >> 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. >> > > > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm >