我使用以下函数删除排名最低的数据(将其标记为NA):
RemoveOutlier<- function(x, pctl){
qnt <- quantile(x, probs = pctl)
y <- x
y[x <= qnt] <- NA
y
} # remove the bottom pctl% data
例如,RemoveOutlier(x=seq(1,10,1), 0.1)
将移除底部10%的数据。
然后我尝试将此功能应用于iris$Petal.Width
Species
,生成新列Petal.Width.Rm
:
iris$Petal.Width.Rm <- with(iris, ave(Petal.Width, Species, FUN = RemoveOutlier(x, 0.1)))
但我找不到错误对象'x'。这有什么问题?
答案 0 :(得分:2)
试试这个
> iris$Petal.Width.Rm <- with(iris, ave(Petal.Width, Species, FUN = function(x) RemoveOutlier(x, 0.1)))
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species Petal.Width.Rm
1 5.1 3.5 1.4 0.2 setosa 0.2
2 4.9 3.0 1.4 0.2 setosa 0.2
3 4.7 3.2 1.3 0.2 setosa 0.2
4 4.6 3.1 1.5 0.2 setosa 0.2
5 5.0 3.6 1.4 0.2 setosa 0.2
6 5.4 3.9 1.7 0.4 setosa 0.4