[R] non-consing count

jim holtman jholtman at gmail.com
Fri Jan 4 16:53:58 CET 2013

What is the concern if it works?  you can also do


Is performance a concern?  How often are you going to do it and what
other parts of your script also take longer?  Why are you concerned
about allocating/discarding two vectors?

On Fri, Jan 4, 2013 at 10:30 AM, Sam Steingold <sds at gnu.org> wrote:
> Hi,
> to count vector elements with some property, the standard idiom seems to
> be length(which):
> --8<---------------cut here---------------start------------->8---
> x <- c(1,1,0,0,0)
> count.0 <- length(which(x == 0))
> --8<---------------cut here---------------end--------------->8---
> however, this approach allocates and discards 2 vectors: a logical
> vector of length=length(x) and an integer vector in which.
> is there a cheaper alternative?
> Thanks!
> --
> Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
> http://www.childpsy.net/ http://iris.org.il http://honestreporting.com
> http://jihadwatch.org http://pmw.org.il http://www.PetitionOnline.com/tap12009/
> War doesn't determine who's right, just who's left.
> ______________________________________________
> R-help at r-project.org 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.

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

More information about the R-help mailing list