1

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

myvec

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

result

AMLM12001KP  AMLM12001KP  1114002  1114002  1121501  1121501  1231401 
1231401  1231401  1231401
MAPK
  • 5,635
  • 4
  • 37
  • 88

1 Answers1

3

We can use na.locf

library(zoo)
na.locf(myvec)
#[1] "AMLM12001KP" "AMLM12001KP" "1114002"     "1114002"     "1121501"    
#[6] "1121501"     "1231401"     "1231401"     "1231401"     "1231401" 
akrun
  • 874,273
  • 37
  • 540
  • 662