Reputation: 1600
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.
EDIT: Thanks to Marius comment I fixed it using a for
loop, might not be the most efficient but it does the job. Someone has a solution in a sapply loop but I could not make it work in my case: Row/column counter in 'apply' functions
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)
a=NULL
atot=NULL
for (i in 1:length(test$value)){
a<-ifelse(abs(test$value[i] - averagetot[i])>0.5, "NA", test$value[i])
atot=c(atot,as.numeric(a))
}
atot[175]
length(atot)
Upvotes: 1
Views: 603
Reputation: 887148
The problem seems to be in the
averagei<-mean(test$value[i-15:i+15])
It should be
averagei<-mean(test$value[(i-15):(i+15)])
Upvotes: 1