Skip to content
Prev 299767 / 398503 Next

Help with vectors and rollapply

Hi Bill

Many thanks for your help.

Cheers
R

-----Original Message-----
From: William Dunlap [mailto:wdunlap at tibco.com]
Sent: 10 July 2012 17:22
To: Raghuraman Ramachandran; r-help at r-project.org
Subject: RE: Help with vectors and rollapply

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