问题描述
这是我的data.table看起来像。 A:E列只是绘制与excel的比较。列 NewShares
是我所需的列。我不要在我的数据中有该列。
ABCDEF
dt< ('
InitialShares级价格金额CashPerShare NewShares
1573.333 0 9.5339 13973.71 0 1573.333
0 1 10.2595 0 .06689 1584.73
0 1 10.1575 0 .06689 1596.33
0 1 9.6855 0 .06689 1608.58')
我试图计算 NewShares
假设通过将股息(
NewShares * CashPershare
)再投资于 InitialShares
90%的价格( Price * .9
)。在excel land中,公式将为第二行的 = F2 +((F2 * E3 * B3)/(C3 * 0.9))
。第一行等于 InitialShares
。
在R land,我试图):
dt [,NewShares:= cumsum(InitialShares [1] * Level * CashPerShare /(Price * .9) InitialShares [1])]
请注意小数点 NewShares
一旦生成字段为了验证您的方法。
如果扩展公式,您会意识到这是有效的:
dt [,NewShares:= cumprod(1 + Level * CashPerShare / Price / 0.9)* InitialShares [1]]
pre>
This is what my data.table looks like. The A:E columns are just to draw comparison to excel. Column
NewShares
is my desired column. I DO NOT have that column in my data.A B C D E F dt<-fread(' InitialShares Level Price Amount CashPerShare NewShares 1573.333 0 9.5339 13973.71 0 1573.333 0 1 10.2595 0 .06689 1584.73 0 1 10.1575 0 .06689 1596.33 0 1 9.6855 0 .06689 1608.58')
I am trying to calculate
NewShares
with the assumption that new shares are added toInitialShares
by reinvesting dividends(NewShares*CashPershare
) at 90% of the price(Price*.9
). In excel land the formula will be=F2+((F2*E3*B3)/(C3*0.9))
as of the second row. The first row is just equal toInitialShares
.In R land, I am trying(which is not quite right):
dt[,NewShares:= cumsum(InitialShares[1]*Level * CashPerShare/(Price*.9)+InitialShares[1])]
Please pay attention to the Decimal points of
NewShares
once you generate the field in order to validate your approach.解决方案If you expand your formula, you'll realize that this works:
dt[, NewShares := cumprod(1+Level*CashPerShare/Price/0.9)*InitialShares[1]]
这篇关于用累积法计算股息的再投资的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!