This question already has an answer here:
这个问题已经有了答案:
- Replacing NAs with latest non-NA value 13 answers
- 将NAs替换为最新的非na值13答案。
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"