[英]Selecting rows where consecutive values change while ignoring NAs
I want to find rows where value changes within a vector that contains NA
s. 我想找到包含NA
的向量中值发生变化的行。 For example, I have a following vector 例如,我有以下向量
x = c(1, 2, 6, 3, 3, 9, 3, 5, 5, 4, 4)
I can use this code which(x[-1] != x[-length(x)]) + 1
and it will give me row numbers when values where changed: 2, 3, 4, 6, 7, 8, 10 . 我可以使用以下代码which(x[-1] != x[-length(x)]) + 1
,当值更改时,它将为我提供行号: 2、3、4、6、7、8、10 。
If I replace x[x>4] <- NA
and rerun the same code, the output will be the row 2 only. 如果我替换x[x>4] <- NA
并重新运行相同的代码,则输出将仅是第2行。 But I want to consider all NA
s as changes except if the previous value is also NA
and deal with the value after as change. 但是我想将所有NA
视为更改, 除非先前的值也是NA
,然后将更改后的值处理。
My desired output for x = c(1, 2, NA, 3, 3, NA, 3, NA, NA, 4, 4)
would be output of following row numbers: 2, 3, 4, 6, 7, 8, 10 我对x = c(1, 2, NA, 3, 3, NA, 3, NA, NA, 4, 4)
期望输出将是以下行号的输出: 2、3、4、6、7、8 10
I would recommend to change NA
with other conventional representation of missing values, such as -9999. 我建议使用其他常规的缺失值表示形式来更改NA
,例如-9999。 After this you can use your method which(x[-1] != x[-length(x)]) + 1
, or try rle
function from base R. 之后,您可以使用方法which(x[-1] != x[-length(x)]) + 1
,或尝试从R rle
函数。
# Sample data
x = c(1, 2, NA, 3, 3, NA, 3, NA, NA, 4, 4)
# Replace missing values with -9999
x[is.na(x)] <- -9999
# Calculate position of non-equal consecutive values
cumsum(rle(x)$length) + 1
# NOTE: you will need to remove last element of the output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.