Re: [R] Replace values in a vector

2009-12-03 Thread Farida Mostajabi
3, 2009 8:41 AM > To: r-help@r-project.org > Subject: [R] Replace values in a vector > > 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

Re: [R] Replace values in a vector

2009-12-03 Thread William Dunlap
> -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Farida Mostajabi > Sent: Thursday, December 03, 2009 8:41 AM > To: r-help@r-project.org > Subject: [R] Replace values in a vector > > Hi all, >

Re: [R] Replace values in a vector

2009-12-03 Thread Farida Mostajabi
WOW! It worked. Thank you! >>> Gabor Grothendieck 12/03/09 11:46 AM >>> na.locf in the zoo package takes the last occurrence and carries it forward into NAs so replace your zeros with NAs and then apply na.locf like this: library(zoo) na.locf(replace(x, x==0, NA)) On Thu, Dec 3, 2009 at

Re: [R] Replace values in a vector

2009-12-03 Thread Gabor Grothendieck
na.locf in the zoo package takes the last occurrence and carries it forward into NAs so replace your zeros with NAs and then apply na.locf like this: library(zoo) na.locf(replace(x, x==0, NA)) On Thu, Dec 3, 2009 at 11:41 AM, Farida Mostajabi wrote: > Hi all, > > I have a vector like this

[R] Replace values in a vector

2009-12-03 Thread Farida Mostajabi
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