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,849
  • 23
  • 46
  • 65
adam.888
  • 7,336
  • 17
  • 63
  • 102

1 Answers1

2

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,127
  • 2
  • 24
  • 45