[R] Newbie needs a count() function

Thomas Lumley thomas at biostat.washington.edu
Tue Mar 28 21:51:50 CEST 2000


On Tue, 28 Mar 2000, Art Salwin wrote:

> Is there a count() function in R that tells how many
> elements of a vector meet a certain criterion?  (Need it for
> generating contour plots.)
> 
> Currently I use the somewhat cumbersome
> count<-length(v[v<7]) to determine, for example, the number
> of elements

sum(v<7) works, too (if there are no NAs in v, which is necessary for your
version too)

If you are worried about NAs then you can do

length(which(v<7))
or
sum( v<7,na.rm=TRUE)


	-thomas

Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list