hi, i need a function that can replace na's in univariate ts objects with a value interpolated linearly using the adjacent non-na values. for example, the function i need (myfun) would do the following> x <- ts(10,11.4,NA,12,9.7) > y <- myfun(x) > y10 11.4 11.7 12 9.7 i can code an na.action method myself to accomplish this, but has someone else already coded routines of this nature? many thanks, robert Robert Sams SANCTUM FI LLP Charles House 18B Charles Street Mayfair London W1J 5DU Tel: +44 (0) 207 667 6360 Dir: +44 (0) 207 667 6363 fax: +44 (0) 207 667 6460 email: robert at sanctumfi.com Authorised and Regulated by the FSA. Sending encrypted mail: See http://pgp.mit.edu (search string 'sanctumfi') for updates. -----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org mQGiBD/xA1kRBAC2RUt8WyHDjXDoO1eu2Mli83cuEV37FicaBs/Wj5ry1QIz1drj ubP25QQvu0lYOOnP7iS48bcOVP77uQYqLbsvzQ4fChFLCg9O3D4ourolZEK69ooJ 74r87PPV/LEnURL4T8E7QCDrRmylJ1iCffgJ9JWkAN4qUG+6fFuOyiqxDwCgiMkM oLaYHjl3unc7Anx59xAlm+MD/R1EdXM9uewCj2kS3SdN+akklg6QVy+gTl3+HGzc MC6ZcLsk1uIklkvCfoYDN7oeC3uVGik7QYkE3n02vfAMGjW7AqsQEoblzi3QscoX Kzitd04NcWeDYIXRThCCydIJ64DdpF293ewJf2fRykmBdAbV0oaqL+zgdrRqFxYJ m2d9A/9m3DoHwXBXPyqueX8naY5hmmeG+mihkdI4H4MBmaPJZW9DnZxor6P2Nm/X muDSf7aZ1t9J0t75oEY/SyjoCYhWlMJS+wnOUq3u5XRNRyo9oI1qQaF5zw8ZmIzW x7nfQMlXpXpETGwbNxZuUoucbN1cDrZBymHxlI5AiBCZ/fya3rQzUm9iZXJ0IFNh bXMgKFNhbmN0dW0gRkkgTExQKSA8cm9iZXJ0QHNhbmN0dW1maS5jb20+iF0EExEC AB0FAj/xA1kFCQHhM4AFCwcKAwQDFQMCAxYCAQIXgAAKCRD3WGga9bCIIPV6AJ9t cDQkN8jW8CxzbU5K2O3dxaMlKwCeNOYQsAgW7S1qAlM345QpDXxbjgC5Ag0EP/ED bRAIANoxq2NjQFdrUMSlQEaitR3pFTmOC7n2rICBAbU/hxlVs1PFSyh6Tr00vzFM py6n4uBCOzrz3b5u5YbRaQzs8ipkqnSzoDD6GKfMEEWYQvZ76kkShWt5zDkLQ2X4 V4X+xJ0iuFT+9cK7VuJ102pjsAwltDUGPKsSwWqs55tzBN8BwAxqNMxRtNYbOTAB Dpnm1BsiZ1TLqHIr4a1t2ZEuqhKV0HEP9VugP9XQz9u1f5QZrriNW/foxBwLuXS7 g+945IGXZq/qxHzgQjJQhC3jIaHUrchrQxy6snoQxgAnuO2/g9SLI8BBsvpVZ+Ac mpkhPtT4pGujwsK/oRFDloAb7BsAAwYIAMlQrB2GPn1ZNFIf9zN+euTv2jWx5Hv4 ZEhqeBTqq00KCT3NSrnOHBTX4x6F4L+ofRzl2L5zIi685wWTpLgqQI7hzKvAxerJ xe1qpz5GfGX976uaqxEfzwQZqcZB2iihhjeOUTxalSWdkX73yNtRmLLikTr0U3E0 v0dB1laMqldYub4X+GeH7tAeQGqYfS6Y+BdNDWfIcwADM0ggLIbNsw+IsjdQNOpq 5R4p1E2o5kfvafIFLpMOZACKKdTBkfiAqOZq8ezDpNHwLrRG4RvQ1K80pGGqaikI XFbJIthvimA5w4MjvenuIn367zj+bz5eFE7YeQ0KG7NAdg2DkxD4W9CITAQYEQIA DAUCP/EDbQUJAeEzgAAKCRD3WGga9bCIIL8RAJ4o9zXtkqK5RMKXxJTmZejtDjTC kwCdFevBc9z4ermWaKb9BsDU7OYdgM8=6Y3T -----END PGP PUBLIC KEY BLOCK-----
Robert Sams wrote:> hi, > > i need a function that can replace na's in univariate ts objects with a value interpolated linearly > using the adjacent non-na values. for example, the function i need (myfun) would do the following > > >>x <- ts(10,11.4,NA,12,9.7) >>y <- myfun(x) >>y > > 10 11.4 11.7 12 9.7 > > i can code an na.action method myself to accomplish this, but has someone else already coded routines > of this nature? > > many thanks, > robert > > > Robert SamsHi Robert, Will the following do? na.approx <- function(x) { na <- is.na(x) if(all(!na)) return(x) i <- seq(along = x) x[na] <- approx(i[!na], x[!na], i[na])$y x } > x <- c(10,11.4,NA,12,9.7) > na.approx(x) [1] 10.0 11.4 11.7 12.0 9.7 --sundar