Hi, I 've a matrix n*1 (thus a column) and I would like to count the number of negative element inside. Can you help me? Thanks! eg: res[,1]= 1 -3 -1 How obtain the number 2 (number of negative-element)?
How about this? length(res[res < 0, 1]) HTH, Andy> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Frederic renaud > Sent: Wednesday, January 05, 2005 11:08 AM > To: r-help at stat.math.ethz.ch > Subject: [R] count element in column > > > Hi, > I 've a matrix n*1 (thus a column) and I would like to > count the number of negative element inside. > Can you help me? > Thanks! > > eg: > res[,1]= 1 > -3 > -1 > > How obtain the number 2 (number of negative-element)? > > ______________________________________________ > 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 >
On 05-Jan-05 Frederic renaud wrote:> Hi, > I 've a matrix n*1 (thus a column) and I would like to > count the number of negative element inside. > Can you help me? > Thanks! > > eg: > res[,1]= 1 > -3 > -1 > > How obtain the number 2 (number of negative-element)?If there is only one column, then either sum(x<0) or sum(x[,1]<0) If there is more than one column, then sum(x[,1]<0) sum(x[,2]<0) ... does it for each column, one at a time, and colSums(x<0) does it for all the columns at once (but separately), while sum(x<0) does it for the whole matrix (without regard for columns). Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 [NB: New number!] Date: 05-Jan-05 Time: 17:43:03 ------------------------------ XFMail ------------------------------
How about sum(res<0)? -- robert.kruus at utoronto.ca The computer should be doing the hard work. That's what it's paid to do, after all. -- Larry Wall in <199709012312.QAA08121 at wall.org> -------- It is rumored that on Wed, 5 Jan 2005 08:07:34 -0800 (PST) Frederic renaud <fren2 at yahoo.com> wrote:> Hi, > I 've a matrix n*1 (thus a column) and I would like to > count the number of negative element inside. > Can you help me? > Thanks! > > eg: > res[,1]= 1 > -3 > -1 > > How obtain the number 2 (number of negative-element)? > > ______________________________________________ > 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 >--------
Something like colSums(res < 0), which works for any number of columns. Andy> From: Frederic renaud > > Hi, > I 've a matrix n*1 (thus a column) and I would like to > count the number of negative element inside. > Can you help me? > Thanks! > > eg: > res[,1]= 1 > -3 > -1 > > How obtain the number 2 (number of negative-element)? > > ______________________________________________ > 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 > >
Have you considered > sum(res[,1]<0) [1] 2 hope this helps. spencer graves Frederic renaud wrote:>Hi, >I 've a matrix n*1 (thus a column) and I would like to >count the number of negative element inside. >Can you help me? >Thanks! > >eg: > res[,1]= 1 > -3 > -1 > >How obtain the number 2 (number of negative-element)? > >______________________________________________ >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 > >
How about sum(res<0)? -- robert.kruus at utoronto.ca The computer should be doing the hard work. That's what it's paid to do, after all. -- Larry Wall in <199709012312.QAA08121 at wall.org> -------- It is rumored that on Wed, 5 Jan 2005 08:07:34 -0800 (PST) Frederic renaud <fren2 at yahoo.com> wrote:> Hi, > I 've a matrix n*1 (thus a column) and I would like to > count the number of negative element inside. > Can you help me? > Thanks! > > eg: > res[,1]= 1 > -3 > -1 > > How obtain the number 2 (number of negative-element)? > > ______________________________________________ > 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 >-------- -- robert.kruus at utoronto.ca A beginning is the time for taking the most delicate care that balances are correct. -- Princess Irulan, "Manual of Maud'Dib"
x<-c(-3. -4.7, -.005, 1, 9)> length(x[x<0])[1] 3 Anne ----- Original Message ----- From: "Frederic renaud" <fren2 at yahoo.com> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, January 05, 2005 5:07 PM Subject: [R] count element in column> Hi, > I 've a matrix n*1 (thus a column) and I would like to > count the number of negative element inside. > Can you help me? > Thanks! > > eg: > res[,1]= 1 > -3 > -1 > > How obtain the number 2 (number of negative-element)? > > ______________________________________________ > 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>