[R] Finding where a string drops below a certain value
Rui Barradas
ruipbarradas at sapo.pt
Sat Mar 9 16:55:57 CET 2013
Hello,
The following functions will return an index to the required values, not
the values themselves.
firstzero <- function(x) which(x == 0)[1]
lastone <- function(x){
z <- firstzero(x)
if(z == 1) NULL else z - 1
}
Hope this helps,
Rui Barradas
Em 09-03-2013 12:53, Williams,Caroline escreveu:
> Dear R-helpers,
> I am stuck on a problem, and hope someone can help. I am trying to find the time point in a time series where the values drop below a baseline threshold, defined by the mean +/- standard deviation calculated on the last 6 time points of the series. Here is a simplified example:
>
> time <- c(1:12)
> x <- c(5.00,4.00,3.00,1.20,1.05,1.20,1.00,-1.00,1.00,-1.00,1.00,-1.00)
>
> min.range <- mean(x[7:12])-sd(x[7:12])
> max.range <- mean(x[7:12])+sd(x[7:12])
>
> flag <- ifelse(x[1:6] > max.range | x[1:6] < min.range, 1,0)
>
> I need to pull out two pieces of information:
> 1) the index for the first time point at which the values return to within one s.d. of the mean of the last 6 values (i.e. the first "0" in the flag list above, returning a time value of 5): this should return the first time point if it is within one s.d. of the mean.
> 2) the number of time points that occur before values return to that range (i.e. the last "1" before the first "0" in the flagged list, returning a value of 4 in this example): I do not want to count any values that go outside the range after the first value within the range, in this case the final "1" in the flagged list). This should return 0 is the first time point is within one s.d of the mean. The time points will not be spaced in increments of 1 as in this example, so I cannot just subtract one from the value in 1, although subtracting 1 from the index point should work.
>
> I hope this is clear, let me know if I have missed any important information. Thank you for your time, any help much appreciated.
> Caroline Williams
>
> __________________________________________________
>
> Caroline Williams, PhD
> Postdoctoral Associate, Hahn lab
> Department of Entomology and Nematology
> University of Florida
> Gainesville, FL
>
> Cell: (352) 262-2908 Office: (352) 273-3949
> Email: carolinewilliams at ufl.edu Website: plaza.ufl.edu/carolinewilliams
>
> ______________________________________________
> R-help at 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.
>
More information about the R-help
mailing list