Skip to content
Prev 241823 / 398500 Next

How to pick out several infinite values when calculating means?

Hi Lei,

Here is one option relying on is.finite()


## Messy data for means
dat <- data.frame(values = c(rnorm(7), 1:7, c(1, 2, 3, NA, 4, 5, 6),
  c(1, 2, Inf, 4, 100, -Inf, NaN)), group = rep(letters[1:4], 7))

## use is.finite() to select for only finite numbers
tapply(dat$values, dat$group, function(x) {mean(x[is.finite(x)])})

if you wanted to just exclude infinite values, but keep NAs (I don't
know why you would but...), you could use !is.infinite() to get values
that are not infinite.

Cheers,

Josh
On Tue, Nov 16, 2010 at 1:24 PM, Lei Zhou <Lei.Zhou at agrsci.dk> wrote: