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.
Asked
Active
Viewed 1.6k times
1 Answers
1
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)
-
rowSums sumup the rows and compare with the cutoff value. I would like to count how many entires in a row is greater than the cutoff value. – satty Jun 22 '15 at 09:47
-
@satty That is what it does. It compares with the cutoff values to get a `TRUE/FALSE` logical matrix and we sum the `TRUE` values to get the count of values greater than cutoff values. Also, please check the code. `rowSums(m1)` and `rowSums(m1 >X)` are different – akrun Jun 22 '15 at 09:57