[R] Filling NA with cumprod?

igorre25 igorre25 at gmail.com
Fri May 25 05:24:38 CEST 2012


Hello,

I need to build certain interpolation logic using R.  Unfortunately, I just
started using R, and I'm not familiar with lots of advanced or just
convenient features of the language to make this simpler.  So I struggled
for few days and pretty much reduced the whole exercise  to the following
problem, which I cannot resolve:

Assume we have a vector of some values with NA:
a <- c(1, 2, 3, NA, NA, 6, 7, NA, NA, 10)

and some coefficients as a vector of the same length:

f <- c(0.9, 1.1, 0.9, 1.1, 0.9, 1.1, 0.9, 1.1, 0.9, 1.1)

I need to come up with function to get the following output

o[1] = a[1]
o[2] = a[2]
o[3] = a[3]
o[4] = o[3]*[f3] # Because a[3] is NA
o[5] = o[4]*[f4] # Because a[4] is NA; This looks like recursive
calculations;  If the rest of the elements we NA, I would use a * c(rep(1,
3), cumprod(f[3:9])), but that's not the case
o[6] = a[6] # Not NA anymore
o[7] = a[7]
o[8] = o[7]*f[7] # Again a[8] is NA
o[9] = o[8]*f[8]
o[10] = a[10] # Not NA

Even though my explanation may seems complex, in reality the requirement is
pretty simple and in Excel is achieved with a very short formula.

The need to use R is to demonstrate capabilities of the language and then to
expand to more complex problems.

Any idea?

Thank you.

--
View this message in context: http://r.789695.n4.nabble.com/Filling-NA-with-cumprod-tp4631269.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list