Re: [R] replacing zeros with above/below numbers ?

2014-02-05 Thread arun
HI,
May be this helps:
a1 <-a


indx <- which(c(0,diff(a!=0)) <0)
indx1 <- which(c(0,diff(a!=0)) >0)
a[a==0] <- rep( rowMeans(cbind(a[indx-1],a[indx1])),indx1-indx)
 a
# [1] 0.97210 0.97220 0.97300 0.97230 0.97145 0.97145 0.97145 0.97060 0.96980
#[10] 0.97040 0.97100 0.96990

#Another option is na.approx() from library(zoo)
 a1[a1==0] <- NA


 na.approx(a1)  ##there is difference in the result though (depends on what you 
want)
 [1] 0.972100 0.972200 0.973000 0.972300 0.971875 0.971450 0.971025 0.970600
 [9] 0.969800 0.970400 0.971000 0.969900
A.K.

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@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.


Re: [R] replacing zeros with above/below numbers ?

2014-02-05 Thread ce

Yes , indeed this is what I am looking for :

a[ a == 0.0 ] = NA
na.approx(a,na.rm="FALSE")

Thanks a lot.

-Original Message-
From: "Jeff Newmiller" [jdnew...@dcn.davis.ca.us]
Date: 02/05/2014 10:35 PM
To: "ce" , "" 
Subject: Re: [R] replacing zeros with above/below numbers ?

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 NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.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  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@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.

__
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.


Re: [R] replacing zeros with above/below numbers ?

2014-02-05 Thread ce

In fact I don't want to replace with average of whole series, just before and 
after values something like this :

for (i in seq(from=2,to=nrow(a)-1,by=1))
{
  if ( a[i] == 0 ) a[i] = ( a[i+1] + a[i-1] ) / 2 
}

but I can't handle gracefully  repeated zeros or  first and last values. 
Jeff proposed na.approx function , I didn't know it, looks promising , I will 
look into it .

ce

-Original Message-
From: "Pascal Oettli" [kri...@ymail.com]
Date: 02/05/2014 10:20 PM
To: "ce" 
CC: "r-help" 
Subject: Re: [R] replacing zeros with above/below numbers ?

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  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@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

__
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.


Re: [R] replacing zeros with above/below numbers ?

2014-02-05 Thread Jeff Newmiller
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 NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.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  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@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.

__
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.


Re: [R] replacing zeros with above/below numbers ?

2014-02-05 Thread Pascal Oettli
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  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@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

__
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.


[R] replacing zeros with above/below numbers ?

2014-02-05 Thread ce

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@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.