本文介绍了滚动累积产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据看起来像这样

    A    B
 1  1    1
 2  3
 3  5
 4  7

我要在B列中计算以下滚动累积乘积.在Excel中,我将具有以下公式

where I want to calculate the following rolling cumulative product in column B. In Excel I would have the following formula

B2 = (1 + A2/10)*B1

到目前为止,我已经尝试使用1 + Ax/100的结果作为一个辅助列,而一个完全相同的列只是被1滞后,然后彼此相乘.不会适用,因为没有应用滚动产品.我也尝试过……

I so far have tried using a helper column that is the results of 1 + Ax/100 , and one exactly the same just lagged by 1 which then gets multiplied by each other. Which wont work as the rolling product isn't applied. I also tried s.th like

rollapply(columnm,1,FUN = prod, fill = NA, align = 'left')

不幸的是,它仍然无法正常工作.

unfortunately it still doesn't work.

我希望得到B的这些结果

I would expect to get these results for B

    A    B
 1  1    1
 2  3    1.3
 3  5    1.95
 4  7    3.315

作为对原始问题的扩展,是否可以在ifelse语句中基于附加列进行相同的滚动乘积运算?

As an extension to the original question, is there a way of doing the same rolling product in an ifelse statement based on an additional column?

    A    B   C
 1  1    1   1
 2  3    1   1
 3  5    0
 4  7    0

相当于excel的

C3 = IF(B3=0,(1+A3/10)*C2,1)

在这种情况下,我无法获得公认的解决方案,因为它始终仅引用A列中的数据,并且最终得到的结果与以前的解决方案相同.

I couldn't get the accepted solution working for this case as it is always referring to just the data in column A and I end up with the same results as in the previous solution.

我希望得到以下结果

    A    B   C
 1  1    1   1
 2  3    1   1
 3  5    0   1.5
 4  7    0   2.55

预先感谢

推荐答案

这是您想要的吗?

A=c(1,3,5,7)
cumprod(c(1,(A[-1]/10+1)))

> cbind(A,B=cumprod(c(1,(A[-1]/10+1))))
     A     B
[1,] 1 1.000
[2,] 3 1.300
[3,] 5 1.950
[4,] 7 3.315

这篇关于滚动累积产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:08