替换长度相同的值> 2个 [英] Replace sequence of identical values of length > 2

查看:65
本文介绍了替换长度相同的值> 2个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测量变量的传感器,当没有连接时,它总是返回最后看到的值而不是NA.因此,在我的向量中,我想用一个推算值(例如,用na.approx)替换这些相同的值.

I have a sensor that measures a variable and when there is no connection it returns always the last value seen instead of NA. So in my vector I would like to replace these identical values by an imptuted 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

但是我只想要大于2的序列(3个或更多相同的数字),因为2个相同的数字可以自然出现. (在前面的示例中,标记顺序为5 5 5)

But I want only the sequences bigger than 2 (3 or more identical numbers) because 2 identical numbers can appear naturally. (in previous example the sequence to tag would be 5 5 5)

我试图用diff来标记我的相同点(c(0, diff(vec) == 0)),但是我不知道如何处理length == 2条件...

I tried to do it with diff to tag my identical points (c(0, diff(vec) == 0)) but I don't know how to deal with the length == 2 condition...

编辑 我的预期输出可能是这样的:

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

(序列等于或大于3的第二个相同值也很可能是错误的值)

(The second identical value of a sequence of 3 or more is very probably a wrong value too)

谢谢

推荐答案

您可以使用rle获取应分配NA的位置的索引.

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

功能上

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

这篇关于替换长度相同的值&gt; 2个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆