+1 vote
3 views
in R Programming by (7.3k points)
edited by

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?

2 Answers

+1 vote
by (25.4k points)
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 

0 votes
by (107k points)

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.

Related questions

+1 vote
2 answers
asked Jul 15, 2019 in R Programming by Ajinkya757 (5.3k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...