Returns the time (sum to position) to a specified value

The time to event represents the sum of positions, in the vector, until the specified value is found ie., (0,0,1) would be 3 or, 2 with up.to=TRUE. The int argument allows for rounding a continuous variable. Since it may be difficult to fine an exact match to a floating point value rounding mitigates the problem. If you want a specific rounding value (eg., 1 decimal place) you can apply it to x first then pass it to the function.

time_to_event(x, y = 1, dir = c("LR", "RL"), int = FALSE, up.to = FALSE)

Arguments

x

A vector, representing time-series, to evaluate

y

Threshold value tor return position for

dir

Direction of evaluation c("LR", "RL")

int

FALSE | TRUE - Evaluate as integer (rounds to 0 decimal places)

up.to

FALSE | TRUE - Return value before event

Value

A vector value representing the time to event

Author

Jeffrey S. Evans <jeffrey_evans@tnc.org>

Examples

library(raster) # Binomial instance time_to_event(c(0,0,0,0,1,0,0,0,1,0))
#> [1] 5
time_to_event(c(0,0,0,0,1,0,0,0,1,0), up.to = TRUE)
#> [1] 4
time_to_event(c(0,0,0,0,1,0,0,0,1,0), dir="RL")
#> [1] 2
r <- do.call(raster::stack, replicate(20,raster::raster(matrix(sample( c(0,1), 1000, replace=TRUE), 100, 100)))) ( t2e <- calc(r, fun=time_to_event) )
#> class : RasterLayer #> dimensions : 100, 100, 10000 (nrow, ncol, ncell) #> resolution : 0.01, 0.01 (x, y) #> extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax) #> crs : NA #> source : memory #> names : layer #> values : 2, 14 (min, max) #>
# Continuous threshold instance ( x <- runif(100, 0,7) )
#> [1] 4.22189523 4.52724934 4.31672756 0.27001891 4.01617737 4.28468772 #> [7] 3.86045417 3.19841076 5.04859500 3.06946302 2.40071753 6.33778185 #> [13] 4.36438634 3.47155253 1.22275435 6.16059056 1.49698242 4.80692251 #> [19] 5.08763741 5.41926183 5.69556307 3.01656034 2.48089580 4.11315308 #> [25] 0.07450751 1.58848645 3.75966879 5.64553650 2.79889624 1.15528882 #> [31] 4.82511322 0.95870079 2.01191785 0.37243058 1.43621046 6.38207603 #> [37] 6.02667199 0.94848120 1.50525433 4.30201261 6.91679739 0.02618177 #> [43] 5.28434486 5.99257844 6.84423880 1.68422542 2.24345676 0.81776021 #> [49] 1.83894428 2.70539222 5.59089807 6.80262377 4.18653360 6.00546687 #> [55] 2.90964909 4.78125007 0.57634292 6.38334392 3.63073704 5.41464169 #> [61] 4.21722458 4.17600321 1.85661126 4.16207615 3.50256902 6.41467098 #> [67] 1.41463633 2.79590890 5.32632177 0.21890119 4.12447328 0.08517882 #> [73] 6.31313306 2.28068044 3.36635507 0.68339149 0.55726945 0.93456627 #> [79] 3.01118174 0.38870338 4.89045025 4.73433872 2.96027734 0.16014138 #> [85] 6.11948113 1.41867028 1.57962462 3.37031520 3.72156478 2.98745638 #> [91] 4.69411627 2.95378887 1.18777857 4.03201406 2.09582595 4.66871990 #> [97] 2.75389826 0.12588129 5.58897285 3.35055411
time_to_event(x, y = 5, int=TRUE)
#> [1] 2
r <- do.call(raster::stack, replicate(20,raster::raster(matrix( runif(1000,0,7), 100, 100)))) t2e <- function(x) { time_to_event(x, y=5, int=TRUE) } ( t2e <- calc(r, fun=time_to_event) )
#> class : RasterLayer #> dimensions : 100, 100, 10000 (nrow, ncol, ncell) #> resolution : 0.01, 0.01 (x, y) #> extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax) #> crs : NA #> source : memory #> names : layer #> values : 2, 4 (min, max) #>