Home > Back-end >  How to count number of values by condition in dataframe?
How to count number of values by condition in dataframe?

Time:08-09

From this dataframe I need to count number of TRUE and FALSE values by columns. However, since I need to automatize the process I cant count them with column names or column indexes. I need something else

df1  <- c(TRUE,TRUE,FALSE,TRUE,TRUE)
df2 <- c(TRUE,FALSE,FALSE,TRUE,TRUE)
df3 <- c(FALSE,TRUE,TRUE,TRUE,TRUE)

df <- data.frame(df1,df2,df3)

Expected outcome:

      df1    df2    df3
 TRUE   4      3      4
FALSE   1      2      1

CodePudding user response:

You could also use table

sapply(df, table)
      df1 df2 df3
FALSE   1   2   1
TRUE    4   3   4

CodePudding user response:

Use colSums:

rbind(nbTRUE = colSums(df), nbFALSE = colSums(!df))
#        df1 df2 df3
#nbTRUE    4   3   4
#nbFALSE   1   2   1

I'd advise against calling object TRUE or FALSE, but you can still do it using backticks:

rbind(`TRUE` = colSums(df), `FALSE` = colSums(!df))

CodePudding user response:

We may unlist and use col index to apply in table

table(unlist(df), c(col(df)))
       
        1 2 3
  FALSE 1 2 1
  TRUE  4 3 4

This would also work in case there are only TRUE or FALSE in a column

df$df4 <- FALSE
table(unlist(df), c(col(df)))
       
        1 2 3 4
  FALSE 1 2 1 5
  TRUE  4 3 4 0
  • Related