In the cases of problems where logical vector contains NA values.
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.
You should be careful with the "table" solution, in case there are no TRUE values in the logical vector.
Suppose a <- c(NA, FALSE, NA) or simply a <- c(FALSE, FALSE)
table(a)["TRUE"] # gives you NA for both cases.