Hi everyone, I would like to have a function to compute some values in a dataset. First, I have to define a value for the lx variable in row 1 (e.g., 100,000), npx is a given proportion. lx of row 2 is equal to lx of row 1 times npx of row 1. I can do this row by row... data[1,"lx"] <- 100000 data[2,"lx"] <- data[1,"lx"]*data[1,"npx"] data[3,"lx"] <- data[2,"lx"]*data[2,"npx"] data[4,"lx"] <- data[3,"lx"]*data[3,"npx"] ... data[19,"lx"] <- data[18,"lx"]*data[18,"npx"] Any ideas about how to define this in a function or in a more systematic way? Thank you in advance. -- Sebasti?n Daza
On 08-02-2012, at 06:23, Sebasti?n Daza wrote:> Hi everyone, > I would like to have a function to compute some values in a dataset. > First, I have to define a value for the lx variable in row 1 (e.g., > 100,000), npx is a given proportion. lx of row 2 is equal to lx of row > 1 times npx of row 1. I can do this row by row... > > data[1,"lx"] <- 100000 > data[2,"lx"] <- data[1,"lx"]*data[1,"npx"] > data[3,"lx"] <- data[2,"lx"]*data[2,"npx"] > data[4,"lx"] <- data[3,"lx"]*data[3,"npx"] > ... > data[19,"lx"] <- data[18,"lx"]*data[18,"npx"] > > Any ideas about how to define this in a function or in a more systematic way?Something like data[,"lx"] <- 100000 * c(1, cumprod(data[,"npx"])) In ypur example column "lx" has length 19 and column "npx" appears to have length 18. So you may have to adjust lengths. Berend
On Feb 8, 2012, at 12:23 AM, Sebasti?n Daza wrote:> Hi everyone, > I would like to have a function to compute some values in a dataset. > First, I have to define a value for the lx variable in row 1 (e.g., > 100,000), npx is a given proportion. lx of row 2 is equal to lx of row > 1 times npx of row 1. I can do this row by row... > > data[1,"lx"] <- 100000 > data[2,"lx"] <- data[1,"lx"]*data[1,"npx"] > data[3,"lx"] <- data[2,"lx"]*data[2,"npx"] > data[4,"lx"] <- data[3,"lx"]*data[3,"npx"] > ... > data[19,"lx"] <- data[18,"lx"]*data[18,"npx"] > > Any ideas about how to define this in a function or in a more > systematic way? > Thank you in advance. >You should get in the habit of offering complete examples. ?cumprod would seem likely helpful. -- David Winsemius, MD West Hartford, CT