lundi 26 octobre 2015

For and if loop in R not working properly [on hold]

I am new to programming. In general, I have time in column mental$Time and I would like to create a vector of values which fall within two standard deviation of the mean.

threshold = (averageT0 + 2*SDT0_n)
threshold_1 = (averageT0 - 2*SDT0_n)
averageSTD=NULL
  for (i in 1:length(mental$Time)) {
     if (mental$Time[i] < threshold | mental$Time[i] > threshold_1)
       averageSTD[i] = mental$Time[i] }
mean(averageSTD, na.rm=TRUE)

but I am getting: Error in if (mental$Time[i] < threshold | mental$Time[i] > threshold_1) averageSTD[i] = mental$Time[i] : missing value where TRUE/FALSE needed

I am not sure what I am doing wrong in the if loop and what I am missing.

Aucun commentaire:

Enregistrer un commentaire