我有一个包含数值和NA的向量。我想要另一个长度相同的向量,由ID组成,该ID每次在原始数字中出现新数字时都会累加一次。
#What I have
have<-c(1.1, NA, 1.1, NA, NA, 1.1, NA,
1.5, NA, 2, NA, 1.5,
NA, 1.1, NA, NA, 1.5, NA)
#What I want
want<-c(1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8)
#Both what I have and want side by side
cbind(want,have)
我尝试过的
#This is pretty far off, it both treats NA's as not duplicated and treats
cbind(have,cumsum(!duplicated(have)))
#This is almost there, but NAs get counted as new groups
cbind(have,rleid(have))
#Can't fill down because some are duplicated between NA's
cbind(rleid(fill(as.data.frame(have),have)$have),have)
这肯定是一个重复的问题,但我找不到正确的东西。
答案 0 :(得分:1)
将NA
转换为cumsum
,然后转换为factor
的情况下,将integer
替换为0怎么办
as.integer(factor(cumsum(replace(have, is.na(have), 0))))
#[1] 1 1 2 2 2 3 3 4 4 5 5 6 6 7 7 7 8 8
尽管它适用于此处给出的数据,但这不是完全证明方法,如果您的数据中实际有0,则它将失败。