Skip to content
Back to formatted view

Raw Message

Message-ID: <8334DEF3-20F8-46CD-8863-323B25346E6E@comcast.net>
Date: 2013-01-04T17:38:46Z
From: David Winsemius
Subject: non-consing count
In-Reply-To: <87d2xlge3a.fsf@gnu.org>

On Jan 4, 2013, at 7:30 AM, Sam Steingold 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?

   I don't know if it is "cheaper", but the way I "learned to count" was:
   sum(x==8, na.rm=TRUE)
   --
   David Winsemius
   Alameda, CA, USA