I wrote a little function called first() to help with situations like 
this.  It returns a 1 every time an element of a vector is different from 
the previous element, and a 0 otherwise.

first <- function(x) {
    L <- length(x)
    c(1, 1-(x[-1]==x[-L]))
    }

sd <- 1
residuals <- c(1, 2.1, 3, 4, 3, 1, 0, -4, -1)
# logical, indicating if the residual exceeds 2 standard deviations
exceed <- abs(residuals) > (2*sd)
# indices of the first exceeding residual of a series
which(first(exceed) & exceed)

Jean


gcm <lvog...@grahamcapital.com> wrote on 07/12/2012 11:07:49 AM:

> I have a graph of residuals and I am attempting to get a list of the 
indexes
> of each time the residual is greater than 2 standard deviations or less 
than
> -2 standard deviations, but only the first point of the section. And 
then
> I'd also need the first point where the point returns to the range 
between
> +/- 2 standard deviations.
> So basically if my standard deviation=1 and my residuals=c(1, 2.1, 3, 4, 
3,
> 1, 0, -4, -1) I want it to grab the second number, where it exceeds 2
> standard deviations and the 6th where it returns to less than 2 standard
> deviations. Also, it would grab -4 (or residuals[ 8]) and -1 
(residuals[9])

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to