[R] sequential treatment of a vector for formula

David Winsemius dwinsemius at comcast.net
Thu May 27 04:50:57 CEST 2010


On May 26, 2010, at 6:24 PM, Frostygoat wrote:

> Please pardon the simplicity of this question of biological nature.
> I'm trying to calculate a statistic, px, the proportion of a cohort
> that survives through the interval x:x+1.  I have the vector from
> which the calc is to be made but I can't figure out how to tell R to
> take the current value and divide it by the next value.
>
> The formula is P0=L1/LO
>
> The following is an example of the lifetable I'm constructing:
>
> example=as.vector(c(8,2,1,5,6,7,7,0,8,10,13,8,11,11,11,2,7,1,5,6,8,6))
> #prime
> k=1
> #Deaths#
> deaths=numeric(k)
> for(k in 0:max(example))
> 	{
> 	deaths[k]=sum(example==k)}
> #adjust for no zero!!#
> deaths=c(0,deaths)
> #Alive, Kx#
> alive=sum(deaths)-cumsum(deaths)
> #Day, or age class,x#
> day=seq(from=0,to=length(deaths)-1)
> #mortality, lx#
> lx=alive/sum(deaths)
> #proportion surviving to next interval, px#
> #####Here's where I'm having trouble!!###
> p=1
> px=numeric(p)
> for(p in 0:length(lx))
> 	{
> 	px[p]=lx/lx}
>
> #create data frame#
> iC=data.frame("x"=day,"Kx"=alive,"Dx"=deaths,"Lx"=lx,"Px"=px)
> print(iC)

iC$Px <- with(iC, c(1, Lx[2:nrow(iC)]/Lx[1:(nrow(iC)-1)] ) )

-- 
David.



More information about the R-help mailing list