Omit inf from row sum in R

后端 未结 5 2047
既然无缘
既然无缘 2020-12-15 18:07

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?

5条回答
  •  遥遥无期
    2020-12-15 18:54

    This is a "non-apply" and non-destructive approach:

    rowSums( matrix(match(A, A[is.finite(A)]), nrow(A)), na.rm=TRUE)
    [1] 2 4
    

    Although it is reasonably efficient, it is not as fast as Johsua's multiplication method.

提交回复
热议问题