[R] cleaning up a vector

Peter Langfelder peter.langfelder at gmail.com
Fri Oct 1 20:00:00 CEST 2010

On Fri, Oct 1, 2010 at 10:51 AM,  <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?

Two steps, starting from vector x

x1 = x[is.finite(x)];
x2 = x1[(x1 <= 20) & (x1 >= 1)];

>From what you say, x2 is the result you want. Just be aware  that
dropping values will change the indexing.


More information about the R-help mailing list