ベクトルをステップ実行して、IQR を使用して範囲を計算し、外れ値を見つけようとしています。IQR の右側の値を探してこのスクリプトを実行すると結果が得られ、左側に実行するとエラーが発生します: TRUE/FALSE が必要な場所に値がありません。データセットで true と false を削除するにはどうすればよいですか? ここに私のスクリプトがあります:
data = c(100, 120, 121, 123, 125, 124, 123, 123, 123, 124, 125, 167, 180, 123, 156)
Q3 <- quantile(data, 0.75) ##gets the third quantile from the list of vectors
Q1 <- quantile(data, 0.25) ## gets the first quantile from the list of vectors
outliers_left <-(Q1-1.5*IQR(data))
outliers_right <-(Q3+1.5*IQR(data))
IQR <- IQR(data)
paste("the innner quantile range is", IQR)
Q1 # quantil at 0.25
Q3 # quantile at 0.75
# show the range of numbers we have
paste("your range is", outliers_left, "through", outliers_right, "to determine outliers")
# count ho many vectors there are and then we will pass this value into a loop to look for
# anything above and below the Q1-Q3 values
vectorCount <- sum(!is.na(data))
i <- 1
while( i < vectorCount ){
i <- i + 1
x <- data[i]
# if(x < outliers_left) {print(x)} # uncomment this to run and test for the left
if(x > outliers_right) {print(x)}
}
そして、私が得るエラーは
[1] 167
[1] 180
[1] 156
Error in if (x > outliers_right) { :
missing value where TRUE/FALSE needed
このスクリプトを実行するとわかるように、右側に 3 つの外れ値が見つかり、エラーがスローされますが、IQR の左側でこれを再度実行すると、ベクトルに 100 の外れ値があります。他の結果が表示されずにエラーが発生します。このスクリプトを修正するにはどうすればよいですか? どんな助けでも大歓迎です。これを修正する方法について、私は何日もウェブと私の本を精査してきました。