Countif function in R

16k views Asked by At

I have a matrix of n*m dimension. I wanted to count the number of columns in a row which has a value greater than "X". How to do this in R? Please let me know.

1

There are 1 answers

2
akrun On

You can try rowSums

 X <- 0.5
 rowSums(m1 > X)

explanation

m1 > X will create a TRUE/FALSE logical matrix. Since TRUE values are treated as 1 and FALSE values are treated as 0, rowSums(m1 > X) will give you a count for each row of the number of values in that row that is greater than X.

data

 set.seed(24)
 m1 <- matrix(rnorm(5*10), ncol=5)