1

For the following two data.frames:

histdevs <- data.frame(a=c(3,8,2), b=c(1,4,6), c=c(6,1,5), d=c(2,4,8), e=c(5,1,5))
newdatadevs <- data.frame(a=c(3), b=c(4), c=c(32), d=c(1), e=c(6))

I want to calculate the variation of the standard deviation over time, let's say in blocks of 3, so I use rollapply in the following loop:

for (i in 1:5) {
Over <- mean(histdevs[,i])+3*sd(histdevs[,i])
Under <- mean(histdevs[,i])-3*sd(histdevs[,i])
checkover<-rollapply(newdatadevs[,i],3,function(x)(ifelse(all(x>Over),1,0)))
checkunder<-rollapply(newdatadevs[,i],3,function(x)(ifelse(all(x<Under),1,0)))
}

I get the following error:

Error in seq.default(start.at, NROW(data), by = by) : 
wrong sign in 'by' argument

Any alternate function that can let me do the same thing is of course welcome as well.

yawgeh
  • 97
  • 1
  • 10
  • The code above uses a width of 1 so its not performing a rolling calculation and so does not require `rollappply`. Also the `all` seems pointless since it is only passing a single value to the anonymous functions. Maybe what you want is `sapply(DF, function(x) as.numeric(x > ... | x < ...))` or perhaps your intention is to do a rolling calculation but in that case its not clear what is wanted. – G. Grothendieck Jul 12 '13 at 10:59
  • Ah yes. The width setting should not be 1, it should be 8. I essentially want to be making sure each value and the consecutive 8 are not outliers according to this. I'm playing around coding a Shewhart process control type check. – yawgeh Jul 12 '13 at 13:28
  • Please state your question in reproducible form so others can replicate the message. – G. Grothendieck Jul 12 '13 at 13:46
  • I think I have done so, or at least provided more information. – yawgeh Jul 12 '13 at 15:48
  • Read this: http://stackoverflow.com/q/5963269 – G. Grothendieck Jul 12 '13 at 16:00
  • Okay, that's done - the code is there and a simple data set that produces the error. – yawgeh Jul 15 '13 at 15:39
  • In your example `newdatadevs[,i]` is a scalar so the whole thing makes no sense. – G. Grothendieck Jul 15 '13 at 20:34
  • In what way specifically does it not make sense? What am I modifying that causes the call to work properly if I replace `newdatadevs` with `newdatadevs <- data.frame(a=c(3,1,7), b=c(4,7,1), c=c(32,78,1), d=c(1,1,58), e=c(6,4,7))` ? – yawgeh Jul 16 '13 at 01:43
  • Ah, I think I just realized the entire problem, I have exactly one instance and want to go row-wise while this function goes column-wise. Thank you for your time/help while I was not great with my question. – yawgeh Jul 16 '13 at 01:47

0 Answers0