I am trying to make a small script to get rid of extreme values in a large dataset, but when my value is "0" my function returns "logical(0)" instead of NA.
#Getting rid of extreme values
test=NULL
test$value<-runif(200,13.90,14.10)
test$date<-seq(1,200,1)
test$value[125]<-15
test$value[175]<-0
plot(test$date, test$value)
averagei=NULL
averagetot=NULL
for (i in 1:length(test$value)) {
averagei<-mean(test$value[i-15:i+15])
averagetot=c(averagetot,averagei)
}
t<-sapply(test$value, function(x) ifelse(x - averagetot[x]>0.5, "NA", x))
t[175]
[[1]]
logical(0)
Why does this happen? I can of course get rid of the 0 before but it drives me crazy that I cannot understand why this happens or fix it.
Aucun commentaire:
Enregistrer un commentaire