My 2 cents: AFAIK both which and length are from C compiled code:
http://cran.r-project.org/doc/manuals/r-release/R-ints.html#g_t_002eInternal-vs-_002ePrimitive so they must be quite efficient ie .Primitive and .Internal. Probably combination of this with a pattern in C would be more memory efficient to count patterns, but would that make sense? Because in general if you look for a pattern in a vector, you need to know where it is, hence which operation, at least for debugging/testing purposes... On 4 January 2013 16:30, Sam Steingold <s...@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@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. ______________________________________________ R-help@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.