[R] count element in column

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Wed Jan 5 18:43:03 CET 2005


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 ------------------------------




More information about the R-help mailing list