R Convert NA's only after the first non-zero value

别说谁变了你拦得住时间么 提交于 2019-12-11 09:24:39

问题


I have a large data set which consists of a columns of IDs followed by a monthly time series for each ID. There are frequent missing values in this set, but what I would like to do is replace all NAs after the first non-zero with a zero while leaving all the NAs before the first non-zero value as NA's.

eg.

[NA NA NA 1 2 3 NA 4 5 NA] would be changed to [NA NA NA 1 2 3 0 4 5 0]

Any help or advice you guys could offer would be much appreciated!


回答1:


Easy to do using match() and numeric indices:

  • use match() to find the first occurence of a non-NA value
  • use which() to convert the logical vector from is.na() to a numeric index
  • use that information to find the correct positions in x

Hence:

x <- c(NA,NA,NA,1,2,3,NA,NA,4,5,NA)
isna <- is.na(x)
nonna <- match(FALSE,isna)
id <- which(isna)
x[id[id>nonna]] <- 0

gives:

> x
 [1] NA NA NA  1  2  3  0  0  4  5  0



回答2:


Here's another method. Convert all to zeros first, then covert the first zeros back to NA.

> x <- c(NA,NA,NA,1,2,3,NA,NA,4,5,NA)
> x[which(is.na(x))] <- 0
### index from 1 to first element before the first element >0
> x[1:min(which(x>0))-1] <- NA
> x
 [1] NA NA NA  1  2  3  0  0  4  5  0

also

### end of vector (elements are >0)
> endOfVec <- min(which(x>0)):length(x)
> x[endOfVec][is.na(x[endOfVec])] <- 0
[1] NA NA NA  1  2  3  0  0  4  5  0


来源:https://stackoverflow.com/questions/20684499/r-convert-nas-only-after-the-first-non-zero-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!