Skip to content

Another way to count TRUE

2 messages · Suharto Anggono Suharto Anggono, Bert Gunter

#
To get the number of TRUE in logical vector 'x',
tabulate(x, 1)
can be used. Actually, it gives count of 1, but TRUE as integer is 1. Since R 3.4.0, it gives a correct answer, too, when the count is 2^31 or more.
#
Bad idea!

In R3.3.3 it doesn't even work:
Error in tabulate(x, 1) : 'bin' must be numeric or a factor

## This does:
But it's more inefficient than just using sum(), even discounting the
as.integer() conversion:
[1] TRUE
user  system elapsed
  2.488   0.003   2.491
user  system elapsed
  0.626   0.001   0.627

Were you simply unaware of sum() or was there some other reason for
your recommendation?

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Jun 7, 2017 at 9:33 AM, Suharto Anggono Suharto Anggono via
R-help <r-help at r-project.org> wrote: