滚动累积产品

编程入门 行业动态 更新时间:2024-10-28 09:27:27
本文介绍了滚动累积产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的数据看起来像这样

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

更多推荐

滚动累积产品

本文发布于:2023-11-22 07:12:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616451.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:产品

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!