How to sum numerical values within a vector?

Viewed 40

I have a vector a containing the following values: 3.00 6.00 NaN -7.00 56.00 32.00 ... Inf 7.00 15.00 NaN Inf 4.00

How is it possible to sum only numerical values (without NaNs, Inf and so on) within such a vector? I've tried sum(a,na.rm=TRUE), but it didn't help me.

1 Answers

You probably want

a <- c(NaN,NA,1,Inf)
sum(a[is.finite(a)])

(sum(a, na.rm=TRUE) takes care of NA and NaN values, but not Inf/-Inf)

This answer is also given here, but the question is not a duplicate ...

Related