R vector:删除以周围值为条件的值

时间:2013-12-06 15:40:07

标签: r vector

这可能不是最好的标题,随时可以编辑它。

 x=c(NA,NA,NA,1,NA,NA,NA,NA,0,NA,NA,NA,1,NA,NA,0,NA,NA,NA,NA,1,NA,NA,NA,0,NA....)

 x=c(NA,NA,NA,0,NA,NA,NA,NA,1,NA,NA,NA,0,NA,NA,1,NA,NA,NA,NA,0,NA,NA,NA,1,NA....)
 y=c(seq(1:length(x)))

我希望z是一个等于y的新向量,除非NA介于0和1之间(不是1和0),它应该重复x = 0时的值

 [1]  1  2  3  4  5  6  7  8  9  9  9  9 13 14 15 16 16 16 16 16 21 22 23 24 25 25

 [1]  1  2  3  4  4  4  4  4  9 10 11 12 13 13 13 16 17 18 19 20 21 21 21 21 25 26

取决于x 我真的不知道如何在R中翻译这个条件。

3 个答案:

答案 0 :(得分:3)

我的解决方案比@ James(现已删除)的答案更笨拙,但也许(?)它更灵活:

## identify strings of NAs preceded by 0
library(zoo)
na_following_zero <- na.locf(c(1,x))[-1]==0 & is.na(x)
## now identify the 'chunks' to reset
## (there may be a more elegant way to do this)
rr <- rle(na_following_zero)
startvals <- cumsum(c(0,rr$lengths))+1
endvals <- cumsum(rr$lengths)
values <- c(NA,y[startvals-1])
z <- y
## replace values in chunks
for (i in seq_along(rr$values)[rr$values])
    z[startvals[i]:endvals[i]] <- values[i]

答案 1 :(得分:2)

如果时间不是很高,你可以使用“for”循环:

z <- y 
between.0.1 <- rep(FALSE, length(x))
for(i in 2:length(x)){
    if(!is.na(x[i-1]) && x[i-1]==0){  # switch on after a 0
        between.0.1[i] <- TRUE  
        value.at.0 <- y[i-1]
        z[i] <- value.at.0
     }
     if(between.0.1[i-1]){  # if switched on, stay switched on 
        between.0.1[i] <- TRUE  
        z[i] <- value.at.0
     }    
     if(!is.na(x[i]) && x[i]==1){  # switch off if at a 1
        between.0.1[i] <- FALSE  
        value.at.0 <- NA
     }
 }    
 z[between.0.1] # diagnostic check

答案 2 :(得分:2)

另一种方法:

 y0 <- which(x==0)
 y1<-which(x==1)
 # need a kicker to start with first zero
 y1<-y1[y1>y0[1]]
 # and check for end of sequence
 if(y1[length(y1)]< length(x)) y1[(length(y1)+1] <- length(x)+1
 #now y0 and y1 better be same length
 z<-y
 #now do a loop any way you want
 for (jj in 1: length(y0) ) z[y0[jj]:(y1[jj]-1)]<-y[y0[jj]]

Rgames> z
 [1]  1  2  3  4  4  4  4  4  9 10 11 12 13 13 13 16 17 18 19 20 21 21 21 21 25
[26] 26