rodrigo guevara
rodrigo guevara

Reputation: 39

Count how many string value appears in a column R Programming

I have this matrix

       [,1]  [,2]  [,3]  [,4]
 [1,] FALSE  TRUE  TRUE  TRUE
 [2,]  TRUE  TRUE FALSE  TRUE
 [3,]  TRUE  TRUE  TRUE  TRUE
 [4,] FALSE  TRUE  TRUE FALSE
 [5,]  TRUE  TRUE  TRUE  TRUE
 [6,]  TRUE  TRUE FALSE  TRUE
 [7,]  TRUE  TRUE FALSE  TRUE
 [8,]  TRUE FALSE  TRUE FALSE
 [9,]  TRUE  TRUE  TRUE  TRUE
[10,]  TRUE  TRUE  TRUE  TRUE

I need to count how many times TRUE and FALSE appears on each of the columns. How can i do that? Thanks

Upvotes: 1

Views: 36

Answers (1)

akrun
akrun

Reputation: 887231

We could use colSums (assuming it is a logical matrix)

n_trues <- colSums(m1)
n_false <- nrow(m1) - n_trues

Or another option is table by column

apply(m1, 2, table)

Upvotes: 2

Related Questions