问题
I have a date frame ("daten"), in which most columns are of numeric value. They typically range from 0 to 5. However, they can also take on the value 99. I want to calculate the mean of the columns, excluding only the values 99.
For example:
> mean(c(0, 1, 2, 3, 4, 5, 99))
[1] 16.28571
is not what I need, instead I want it to be calculated as if the vector was
> mean(c(0, 1, 2, 3, 4, 5))
[1] 2.5
, giving me the mean I am searching for.
There has been a similar question (Calculate mean, median by excluding any given number), but the solution does not work for me. I figured, however, that once I can exclude a certain value in any column, I can simply combine it with apply
, so I am actually looking for a way to calculate a mean for a certain vector, but ignoring certain values.
回答1:
We can replace
the value '99' with NA and get the mean
with na.rm = TRUE
mean(replace(v1, v1==99, NA), na.rm = TRUE)
#[1] 2.5
data
v1 <- c(0, 1, 2, 3, 4, 5, 99)
回答2:
You can also try this:
vec1 <- c(0, 1, 2, 3, 4, 5, 99)
mean(vec1[which(vec1!=99)]
#[1] 2.5
来源:https://stackoverflow.com/questions/46974440/calculate-mean-and-other-excluding-certain-values