0

I can remove na values from a vector:

na.omit(c(1,2,NA,3))

But how can I remove Inf and -Inf?

na.omit(c(1,2,NA,3,Inf))
na.omit(c(1,2,NA,3,-Inf))
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
adam.888
  • 7,686
  • 17
  • 70
  • 105

1 Answers1

3

Remember that is.na and is.infinite may operate on vectors, returning vectors of booleans. So you can filter the vector as so:

> x <- c(1, 2, NA, Inf, -Inf)
> x[!is.na(x) & !is.infinite(x)]
[1] 1 2

If this needs to be done inline, consider putting the above in a function.

Will Beason
  • 3,417
  • 2
  • 28
  • 46