r - How to count TRUE values in a logical vector -
In R, what is the most effective / idiomatic way to calculate the number of TRUE
Logical vector? I can think of two ways:
z
Who likes? Is anything better?
There are some problems when the NA
value in the logical vector.
For example, see:
z So I think that's safe
sum (z, na.rm = TRUE)
(which returns 1). I think the table
solution is less efficient (see the code for the table
function).
Also, if you do not have a "table" solution, if there are no real values in the logical vector z or simply z < -c (wrong, false)
Table (Z) ["TRUE"] # gives you NA for both cases
Comments
Post a Comment