Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Omit inf from row sum in R

Tags:

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?

like image 613
Lcat91 Avatar asked Mar 13 '13 18:03

Lcat91


People also ask

Is infinite in R?

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.


1 Answers

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) 
like image 87
Joshua Ulrich Avatar answered Oct 21 '22 12:10

Joshua Ulrich