[R] Fill in NA values in vector with previous character/factor

Owen Jones owen.jones at imperial.ac.uk
Mon Jul 28 20:10:34 CEST 2008


I have a vector of data (species names) interspersed with NA values  
and I want a function to "fill in the blanks", replacing NA values  
with whatever the last species name was.

For example the vector:

  "A","B",NA,NA,"C",NA,NA,NA,NA,"D",NA,NA.

should evaluate to:

"A" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D"


I tried to use rle() in a function to do this but have hit a brick wall.

How would YOU do this?

Many thanks,

Owen



More information about the R-help mailing list