It looks like you already have the zoo package loaded so you can use its 
na.locf(),
which replaces NA's with the last non-NA value.  Convert the 0s to NAs with
replace() and feed the result into na.locf():
  a  <- c(-2,0,0,0,1,0,0,3,0,0,-4)
  aOut <- c(-2,-2,-2,-2,1,1,1,3,3,3,-4)
  na.locf(replace(a, a==0, NA) )
  #  [1] -2 -2 -2 -2  1  1  1  3  3  3 -4
  all.equal(aOut, .Last.value)
  # [1] TRUE

If you need to treat NA and 0 differently you will need to do more work.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
> Behalf Of Raghuraman Ramachandran
> Sent: Tuesday, July 10, 2012 8:23 AM
> To: r-help@r-project.org
> Subject: [R] Help with vectors and rollapply
> 
> Hello
> 
> I have a vector a =(-2,0,0,0,1,0,0,3,0,0,-4)
> 
> I want to replace all zeros into previous non-zero state. So for instance the 
> above
> vector should be converted into:
> 
> a= (-2,-2,-2,-2,1,1,1,3,3,3,-4)
> 
> I tried many things and finally concluded that probably(?) rollapply may be 
> the best
> way?
> 
> I tried
> f= function(x){
>                 ifelse(x==0,Lag(x),x)
> }
> 
> And then, rollappy(a,1,f) and that didn't work. Can someone help please?
> 
> Thx
> R
> 
> 
> Jefferies archives and monitors outgoing and incoming e-mail. The contents of 
> this
> email, including any attachments, are confidential to the ordinary user of 
> the email
> address to which it was addressed. If you are not the addressee of this email 
> you may
> not copy, forward, disclose or otherwise use it or any part of it in any form
> whatsoever. Since the confidentiality of Internet e-mail cannot be guaranteed,
> please do not include private or confidential information (such as account 
> numbers)
> or instructions requiring your authorization (such as orders or funds 
> transfers) in your
> e-mail communication to us. This email may be produced at the request of 
> regulators
> or in connection with civil litigation. Jefferies accepts no liability for 
> any errors or
> omissions arising as a result of transmission. Although this transmission and 
> any
> attachments are believed to be free of any virus or other defect that might 
> affect any
> computer system into which it is received and opened, it is the!
>   responsibility of the recipient to ensure that it is virus free and no 
> responsibility is
> accepted by Jefferies, its subsidiaries and affiliates, as applicable, for 
> any loss or
> damage arising in any way from its use. In the United Kingdom, Jefferies 
> operates as
> Jefferies International Limited; registered in England: no. 1978621; and 
> Jefferies
> Bache Limited; registered in England: no. 114226; registered office for both: 
> Vintners
> Place, 68 Upper Thames Street, London EC4V 3BJ. Jefferies International 
> Limited and
> Jefferies Bache Limited are authorised and regulated by the Financial Services
> Authority. If you received this transmission in error, please immediately 
> contact the
> sender and destroy the material in its entirety, whether in electronic or 
> hard copy
> format. Thank you.
> 
>       [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

Reply via email to