+1 vote
4 views

edited

In R, what is the most efficient/idiomatic way to count the number of TRUE values in a logical vector? I can think of two ways:

z <- sample(c(TRUE, FALSE), 1000, rep = TRUE)

sum(z)

# [1] 498

table(z)["TRUE"]

# TRUE

#  498

Which do you prefer? Is there anything even better?

+1 vote

edited by

To count TRUE values in a logical vector, you can use the following functions:

z <- sample(c(TRUE, FALSE), 10000, rep = TRUE)

length(z[z== TRUE])

[1] 5095

sum(z)

[1] 5095

table(z)["TRUE"]

TRUE

5095

In vector where there are NA values:

z <- sample(c(TRUE, FALSE,NA), 10000, rep = TRUE)

sum(z, na.rm = TRUE)

[1] 3265

table(z)["TRUE"]

TRUE

3265

The length function counts the return value of NA’s as a TRUE value.

length(z[z== TRUE])

[1] 6671

In vectors having no TRUE values:

sum(z, na.rm = TRUE)

[1] 0

length(z[z== TRUE])                     #Counts return values of NA as TRUE

[1] 5004

table(z)["TRUE"]                          #Returns NA

<NA>

NA

So the best solution to count TRUE values is to use the sum() function along with the na.rm argument as TRUE.

If you want to learn more about R programming watch this tutorial on Introduction to Data Science with R

by (106k points)
edited by

To find the true values in R where logical vector contains NA values you can use the following code:-

a <- c(TRUE, FALSE, NA)

sum(a) # gives you NA

table(a)["TRUE"] # gives you 1

length(a[a==TRUE]) # f3lix answer, gives you 2 (because NA indexing returns values)

sum(a, na.rm=TRUE) # best way to count TRUE values #which gives 1.

If in case there are no TRUE values in the logical vector. You should be careful with the "table" solutions.

Suppose a <- c(NA, FALSE, NA) or simply a <- c(FALSE, FALSE)

table(a)["TRUE"] # gives you NA for both cases.

+1 vote