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?
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- How to insert pictures into each individual bar in
This is a "non-apply" and non-destructive approach:
Although it is reasonably efficient, it is not as fast as Johsua's multiplication method.
Try this...
Or
I'd use
apply
andis.infinite
in order to avoid replacingInf
values byNA
as in @Hemmo's answer.Some benchmarking for comparison:
So Joshua's method wins! And apply method is clearly slower than two other methods (relatively speaking of course).
Multiply your matrix by the result of
is.finite(m)
and callrowSums
on the product withna.rm=TRUE
. This works becauseInf*0
isNaN
.