Dear all, My data is : a <- c(0.9721,0.9722,0.9730,0.9723,0.0,0.0,0.0,0.9706,0.9698,0.0,0.9710,0.9699) I want to replace zeros with average of before and after values of them. But sometimes there is one zero sometimes more than one. What is the most elegant way to do this ? Thanks a lot
Hello, If you mean replacing 0 by the average of non-zero values, I guess one way is:> a[a==0] <- mean(a[a!=0])Maybe some senior user might correct it. Regards, Pascal On 6 February 2014 12:05, ce <zadig_1 at excite.com> wrote:> > Dear all, > > My data is : > > a <- c(0.9721,0.9722,0.9730,0.9723,0.0,0.0,0.0,0.9706,0.9698,0.0,0.9710,0.9699) > > I want to replace zeros with average of before and after values of them. But sometimes there is one zero sometimes more than one. What is the most elegant way to do this ? > Thanks a lot > > ______________________________________________ > 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.-- Pascal Oettli Project Scientist JAMSTEC Yokohama, Japan
You seem to be treating zeroes as unknown values. Perhaps you should consider setting them to NA and using the na.approx function from the zoo package. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On February 5, 2014 7:05:46 PM PST, ce <zadig_1 at excite.com> wrote:> >Dear all, > > My data is : > >a <- >c(0.9721,0.9722,0.9730,0.9723,0.0,0.0,0.0,0.9706,0.9698,0.0,0.9710,0.9699) > >I want to replace zeros with average of before and after values of >them. But sometimes there is one zero sometimes more than one. What is >the most elegant way to do this ? >Thanks a lot > >______________________________________________ >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.