So I am trying to sum the rows of a matrix, and there are inf's within it. How do I sum the row, omitting the inf's?
Multiply your matrix by the result of is.finite(m) and call rowSums on the product with na.rm=TRUE. This works because Inf*0 is NaN.
is.finite(m)
rowSums
na.rm=TRUE
Inf*0
NaN
m <- matrix(c(1:3,Inf,4,Inf,5:6),4,2) rowSums(m*is.finite(m),na.rm=TRUE)
2.1m questions
2.1m answers
60 comments
57.0k users