[R] Replace values in a vector

Farida Mostajabi f0most01 at louisville.edu
Thu Dec 3 17:41:21 CET 2009


Hi all,

I have a vector like this:

x<- c(0.7, 0.1, 0, 0.2, 0.2, 0, 0, 0 , 0, 0.4, 0, 0.8, 1.8)

I would like  to replace the zero values with the first previous non zero value.

my returning vector should look like this:

y<-c( 0.7, 0.1, 0.1,0.2,0.2,0.2,0.2,0.2, 0.4, 0.4, 0.8, 1.8)

How can I do this in R without using for loop?

Thank you




More information about the R-help mailing list