[R] cleaning up a vector

Marc Schwartz marc_schwartz at me.com
Fri Oct 1 20:00:42 CEST 2010


On Oct 1, 2010, at 12:51 PM, mlarkin at rsmas.miami.edu wrote:

> I calculated a large vector.  Unfortunately, I have some measurement error
> in my data and some of the values in the vector are erroneous.  I ended up
> wih some Infs and NaNs in the vector.  I would like to filter out the Inf
> and NaN values and only keep the values in my vector that range from 1 to
> 20.  Is there a way to filter out Infs and NaNs in R and end up with a
> clean vector?
> 
> Mike


set.seed(1)
x <- sample(c(0:25, NaN, Inf, -Inf), 50, replace = TRUE)

> x
 [1]    7   10   16  NaN    5  NaN  Inf   19   18    1    5    5   19
[14]   11   22   14   20 -Inf   11   22  Inf    6   18    3    7   11
[27]    0   11   25    9   13   17   14    5   23   19   23    3   20
[40]   11   23   18   22   16   15   22    0   13   21   20


> x[is.finite(x) & x >= 1 & x <= 20]
 [1]  7 10 16  5 19 18  1  5  5 19 11 14 20 11  6 18  3  7 11 11  9 13
[23] 17 14  5 19  3 20 11 18 16 15 13 20



See ?is.finite

HTH,

Marc Schwartz



More information about the R-help mailing list