我一直在尝试根据不同长度的间隔计算数据框或列表中顺序项的乘积。本质上,我想从给定不规则间隔大小的 Qx 值列表中计算 nQx。
dComp <- data.frame(AGE = seq(0,74), MORTALITY=c(869,58,40,37,36,35,32,28,29,23,24,22,24,28,
33,52,57,77,93,103,103,109,105,114,108,112,119,125,117,127,125,134,134,131,152,179,173,
182,199,203,232,245,296,315,335,356,405,438,445,535,594,623,693,749,816,915,994,1128,1172,
1294,1473,1544,1721,1967,2129,2331,2559,2901,3203,3470,3782,4348,4714,5245,5646)/100000)
x <- c(0,1,5,10,15,20,25,30,35,40,45,50,55,60,65,70)
n <- c(diff(x),999)
n
[1] 1 4 5 5 5 5 5 5 5 5 5 5 5 5 5 999
对于 1 个项目,我能够找到值:
首先计算 Px:
Px <- sapply(dComp$MORTALITY, function(Qx) (1 - Qx))
对于 x = [1,4] 的区间
1- prod(Px[2:5])
您如何在整个间隔列表中实现这一点。在 vba 中我会使用 for 循环,但我知道在 R 中使用了应用程序。PS:有人能推荐一本好的R指令书吗?