Count of TRUE and FALSE

Asked

Viewed 743 times

3

I have a vector with only true and false, would like to know how do I count how many true and false have in this vector?

  • 1

    Try to post part of the code and the difficulty you had.

  • Share source code if possible.

3 answers

5

Use the Summary function(),

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 

3

I would use the function table

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

Upshot:

x
FALSE  TRUE 
    1     2 

3

In addition to the simple answers that have already been given, there is one more complicated that can be useful when we only need to know one of or how many TRUE or how many FALSE.
For this, you can use the function sum. Like FALSE/TRUE is coded internally 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 deny x, thus FALSE becomes TRUE, that is to say 1:

sum(!x)    # negação de x
#[1] 1
  • 1

    And mean(x) results in the proportion of TRUE in the vector.

  • @Tomásbarcellos Certo. I did not include in the answer because that was not the question.

  • 1

    Sure, it was just to add to future readers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.