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?
infinite() Function. is. infinite() Function in R Language is used to check if the vector contains infinite values as elements. It returns a boolean value for all the elements of the vector.
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
.
m <- matrix(c(1:3,Inf,4,Inf,5:6),4,2) rowSums(m*is.finite(m),na.rm=TRUE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With