count of TRUE and FALSE

3

I have a vector with just TRUE and FALSE and would like to know how I can count how many TRUE and FALSE I have in this vector?

    
asked by anonymous 03.05.2018 / 22:54

3 answers

5

Use the function summary (),

I<-sample (c(0,1), 8, T)
V<-I==1

So I created a TRUE FALSE vector

> summary(V)
   Mode   FALSE    TRUE 
logical       6       2 
    
03.05.2018 / 23:06
3

I would use the function table

x <- c(TRUE, TRUE, FALSE)
table(x)

Result:

x
FALSE  TRUE 
    1     2 
    
03.05.2018 / 23:09
3

In addition to the simple answers that have already been given, there is a more complicated answer that can be useful when you only need to know one or how many TRUE or how many FALSE . For this, you can use the sum function. Since FALSE/TRUE is internally encoded as 0/1 , we can add logical values.

x <- c(TRUE, TRUE, FALSE)    # dados do Marcos Banik

To see how many TRUE :

sum(x)
#[1] 2

To see how many FALSE we deny x , so FALSE becomes TRUE , that is 1 :

sum(!x)    # negação de x
#[1] 1
    
04.05.2018 / 12:55