Skip to content

Commit

Permalink
- Extracts longest run instead of just trimming tails.
Browse files Browse the repository at this point in the history
  • Loading branch information
joethorley committed Feb 3, 2024
1 parent e358186 commit abdf19b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 1 addition & 3 deletions R/gsdd-cf.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ gsdd_cf <- function(x,
if(length(x) < 184) {
return(NA_real_)
}
if(anyNA(x)) {
x <- trim_na(x)
}
x <- longest_run(x)
if(length(x) < 184 || anyNA(x)) {
return(NA_real_)
}
Expand Down
16 changes: 9 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
trim_na <- function(x) {
indices <- which(!is.na(x))
if(!length(indices)) {
return(x[0])
longest_run <- function(x) {
rle <- rle(is.na(x))
wch <- which.max(rle$length)
if(rle$length[wch] < 184 | rle$values[wch]) {
return(NA_real_)
}
first <- indices[1]
last <- indices[length(indices)]
x[first:last]
cumsum <- cumsum(rle$lengths)
to <- cumsum[wch]
from <- if(wch == 1) 1L else cumsum[wch-1] + 1L
x[from:to]
}

sum_vector <- function(from, to, ..vector) {
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test-gsdd-cf.R
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ test_that("NA if less than 184 values after trimming trailing NAs", {
})

test_that("extracts longest non-missing sequence (not just trim tails)", {
x <- c(NA,1,NA,rep(1,184),NA)
expect_identical(gsdd_cf(x),NA_real_) # should be 0
x <- c(NA,1,NA,rep(1,183),NA,1,NA)
expect_identical(gsdd_cf(x),NA_real_)
x <- c(NA,1,NA,rep(1,184),NA,1,NA)
expect_identical(gsdd_cf(x),0)
})

0 comments on commit abdf19b

Please sign in to comment.