I have the following list of vectors:
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 to keep vectors that meet certain run-length condition.
Keep the vectors with at least 4 adjacent non-0 values (it works)
## first, second and third vectors
dummy_list2 <- Filter(function (x) any(with(rle(x), lengths[values! = 0] >= 4)),
dummy_list)
Keep vectors which contain something else than 0s (it works)
## first, second and third vectors
dummy_list3 <- Filter(function (x) which(sum(x) != 0),
dummy_list)
Keep vectors with less than 5 adjacent non-0 values (it doesn't work)
## should contain third and fourth vectors
## but gives me first and third vectors
dummy_list4 <- Filter(function(x) any(with(rle(x), lengths[values != 0] < 5)),
dummy_list)
How to make it work?