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
Try this: x[is.finite(x)] On Fri, Oct 1, 2010 at 2:51 PM, <mlarkin@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 > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Mike, Small, reproducible examples are always useful for the rest of the us. x <- c(0, NA, NaN, 1 , 10, 20, 21, Inf) x[!is.na(x) & x >=1 & x<= 20] Is that what you're looking for? 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
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 thatdropping values will change the indexing. Peter
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? > > Mikeset.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