I would like to write a function which would allow to filter the input data. My input data is a list object containing named numeric vectors (minimal reproducible example below - dummy list).
vec1 <- c(rep(0, 10), rep(1, 4), rep(0,5), rep(-1,5))
vec2 <- c(rep(-1, 7), rep(0,99), rep(1, 6))
vec3 <- c(rep(1,2), rep(-1,2), rep(0,10), rep(-1,4), rep(0,8))
vec4 <- rep(0, 100)
dummy_list <- list(vec1, vec2, vec3, vec4)
names(dummy_list) <- c("first", "second", "third", "fourth")
I want my function to test whether in this vector any non-zero value occurs at least 5 times in a row.
My desired output should be a list containing only first two vectors of the initial dummy_list.
Below is one of my multiple attempts - I would like it to be as much similar to this as possible (except that the solution should work).
dummy_list <- Filter(function(x) which(rle(x$values !=0) x$lengths>5, dummy_list)
Note that we check whether any of the the rle length
is greater or equal to 5.
Filter(function(x)any(with(rle(x), lengths[values!=0]>=5)), dummy_list)
$first
[1] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 -1 -1 -1 -1 -1
$second
[1] -1 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[32] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[63] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[94] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1