Replace a sequence of equal length values> 2
I have a sensor that measures a variable, and when there is no connection, it always returns the last value instead NA
. So in my vector, I would like to replace these identical values ββwith a value (for example with na.approx
).
set.seed(3)
vec <- round(runif(20)*10)
#### [1] 2 8 4 3 6 6 1 3 6 6 5 5 5 6 9 8 1 7 9 3
But I only want sequences larger than 2 (3 or more identical numbers) because 2 identical numbers can appear naturally. (in the previous example, the sequence for the tag would be 5 5 5
)
I tried to do it with diff
to mark my same dots ( c(0, diff(vec) == 0)
), but I don't know how to deal with the condition length == 2
...
EDIT my expected output could be like this:
#### [1] 2 8 4 3 6 6 1 3 6 6 5 NA NA 6 9 8 1 7 9 3
(A second identical sequence value of 3 or more is probably also a wrong value)
thank
source to share
you can use rle
to get the indices of the positions where NA
should be assigned.
vec[with(data = rle(vec),
expr = unlist(sapply(which(lengths > 2), function(i)
(sum(lengths[1:i]) - (lengths[i] - 2)):sum(lengths[1:i]))))] = NA
vec
#[1] 2 8 4 3 6 6 1 3 6 6 5 NA NA 6 9 8 1 7 9 3
In function
foo = function(X, length){
replace(x = X,
list = with(data = rle(X),
expr = unlist(sapply(which(lengths > length), function(i)
(sum(lengths[1:i]) - (lengths[i] - length)):sum(lengths[1:i])))),
values = NA)
}
foo(X = vec, length = 2)
#[1] 2 8 4 3 6 6 1 3 6 6 5 NA NA 6 9 8 1 7 9 3
source to share