我试图通过矢量来找到使用IQR计算范围的异常值。当我运行这个脚本寻找IQR右侧的值时,我得到了结果,当我向左运行时,出现错误:缺少值,其中TRUE/FALSE所需。我怎样才能清除数据集中的真假? 这里是我的脚本:在[R]中出错 - 需要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个异常值时,我只是得到错误而没有显示其他结果。 我该如何修复这个脚本?任何帮助不胜感激。我一直在网上和我的书上搜索如何解决这个问题。
'i = 16'发生错误。比较后切换'i <-i + 1'。 – liuminzhao
您正在切换该语句的位置,以便在比较后发生。 –
是的,谢谢,修好了! –