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

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon, 28 Jul 2008 15:24:15 -0400

Try this:

library(zoo)
x <- c("A","B",NA,NA,"C",NA,NA,NA,NA,"D",NA,NA)

na.locf(x)

On Mon, Jul 28, 2008 at 2:10 PM, Owen Jones <owen.jones_at_imperial.ac.uk> wrote:
> 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
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 28 Jul 2008 - 19:36:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Jul 2008 - 20:34:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive