[R] Odp: Vector replace 0 elements without using a loop
Petr PIKAL
petr.pikal at precheza.cz
Thu Oct 7 09:12:44 CEST 2010
Hi
r-help-bounces at r-project.org napsal dne 07.10.2010 07:16:01:
>
> Hi,
>
> With a vector like:
>
> x = c (22, 23, 22.5, 0,0,24, 0, 23.2, 23.5, 0,0,0, 26)
>
> How can I replace the 0's with the previous last value without looping
> through the vector ?
Change your zeroes to NA values
x[x==0]<-NA
use na.locf function from zoo package
library(zoo)
x<-na.locf(x)
Regards
Petr
>
> Something tells me I am missing the obvious.
>
> Thanks,
> Chris
> --
> View this message in context:
http://r.789695.n4.nabble.com/Vector-replace-0-
> elements-without-using-a-loop-tp2966191p2966191.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list