Hi,
I have a problem to calculate the following thing: I have subjects
(IdS) at different ages (age) and with a nominal variable
(T0). I also have a rate r.
The aim si to calculate: $T0 \times \sum_{i=1}^{Age} (1+r)^i$
For instance:
> r_2
> data_data.frame(IdS=rep(1:3,rep(3,3)),T0=rep(c(100,150,120),each=3)
,Age=c(1,3,4,1,2,4,2,3,4))
> data[,4]_ifelse(data$Age==1,data$T0*(1+r),
ifelse(data$Age==2,data$T0*((1+r)+(1+r)^2),
ifelse(data$Age==3,data$T0*((1+r)+(1+r)^2+(1+r)^3),
data$T0*((1+r)+(1+r)^2+(1+r)^3+(1+r)^4)
)))
For small values of "age", this code is reasonnable... My problem is
that the age can reach 15 or 20... and I have 20,000 subjects.
Is there a simpler way to calculate this quantity?
Thank you in advance,
Tristan Lorino
--
Laboratoire Central des Ponts et Chaussées
[Division ESAR ? Section AGR]
Route de Bouaye BP 4129
44341 Bouguenais Cedex
France
Tél 33 (0)2 40 84 56 18
Fax 33 (0)2 40 84 59 92
|