如何交错矢量项,以R中的前值替换NAs [duplicate]

时间:2021-10-10 23:01:44

This question already has an answer here:

这个问题已经有了答案:

I have this vector myvec. I want to remove all the NAs and replace them with the preceding items and get the result.

有这个向量myvec。我想删除所有的NAs并用前面的项替换它们,然后得到结果。

myvec

myvec

c("AMLM12001KP", NA, "1114002", NA, "1121501", NA, "1231401", NA, NA, NA)

result

结果

AMLM12001KP  AMLM12001KP  1114002  1114002  1121501  1121501  1231401 1231401  1231401  1231401

1 个解决方案

#1


3  

We can use na.locf

我们可以使用na.locf

library(zoo)na.locf(myvec)#[1] "AMLM12001KP" "AMLM12001KP" "1114002"     "1114002"     "1121501"    #[6] "1121501"     "1231401"     "1231401"     "1231401"     "1231401" 

#1


3  

We can use na.locf

我们可以使用na.locf

library(zoo)na.locf(myvec)#[1] "AMLM12001KP" "AMLM12001KP" "1114002"     "1114002"     "1121501"    #[6] "1121501"     "1231401"     "1231401"     "1231401"     "1231401"