MrMax
MrMax

Reputation: 393

mean of vector with at least n (>1) non-missing values, R

I want to calculate the mean of a vector with missing values only if at least a number of values exist. So for example if only one (or less than five) value(s) are non-missing, I want to return NA, but if I have at least two (or more than five) non-missing values, I want to return the mean.

How could I achieve this using mean()?

The standard behavior of mean() is to either return the NA if there is at least one value missing or return the mean if at least one value is non-missing (I want something in between):

x = c(1, 2, NA, NA)

mean(x)   # returns NA if there is one (or more) value missing
[1] NA

mean(x, na.rm = T) # returns the mean of the existing values (if there is at least one)
[1] 1.5

Ideally I would want a solution that would work for a tapply(..., FUN = mean) scenario.

Upvotes: 0

Views: 946

Answers (1)

Cettt
Cettt

Reputation: 11981

here is one way using the ifelsefunction:

x <- c(rep(NA_real_, 5), 1:4)

mean(x, na.rm = T)
[1] 2.5
ifelse(sum(!is.na(x)) > 5, mean(x, na.rm = T), NA_real_)
[1] NA

Upvotes: 1

Related Questions