Dear Marc,

Try

require(zoo)
na.locf(a)

HTH,
Jorge.-


On Thu, Nov 22, 2012 at 11:56 PM, Marc Girondot <> wrote:

> Dear members,
>
> I have a series of values in a vector and some value are missing and
> replaced with NA.
> For example:
> a <- c(27, 25, NA, NA, 24, 26, 27, NA, 26)
> I would like to replace the NAs with the value taken from the previous
> value that is non-NA. The output would be in this case:
>
> 27 25 25 25 24 26 27 27 26
>
> Now I do that with a for loop, but I try to eliminate all the loops to
> gain in performance
>
> I try this and it works but again I have a while and then I do not
> eliminate completely loop:
>
> l <- length(a)
> while(any(is.na(a))) {a[2:l] <- ifelse(is.na(a[2:l]), a[1:(l-1)], a[2:l])}
>
> If someone have another solution, I will be most happy !
>
> Sincerely
>
> Marc Girondot
>
> --
> ______________________________**____________________________
> Marc Girondot, Pr
>
> Laboratoire Ecologie, Systématique et Evolution
> Equipe de Conservation des Populations et des Communautés
> CNRS, AgroParisTech et Université Paris-Sud 11 , UMR 8079
> Bâtiment 362
> 91405 Orsay Cedex, France
>
> Tel:  33 1 (0)1.69.15.72.30   Fax: 33 1 (0)1.69.15.73.53
> e-mail: marc.giron...@u-psud.fr
> Web: 
> http://www.ese.u-psud.fr/epc/**conservation/Marc.html<http://www.ese.u-psud.fr/epc/conservation/Marc.html>
> Skype: girondot
>
> ______________________________**________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide http://www.R-project.org/**
> posting-guide.html <http://www.R-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to