Calculate mean and other excluding certain values [duplicate]

China☆狼群 提交于 2020-05-09 07:48:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!