R Statistics question:

I am using the projection.matrix() function from the "popbio" package to create transition matrices. In the function, you have to specify the "stage" and "fate" (both categorical variables), and the "fertilities" (a numeric column).

Everything works fine, but I would like to apply the function to 1:n fertility columns within the data frame, and get a list of matrices generated from the same categorical variables with the different fertility values.

This is how my data frame looks like (I only include the variables I am using for this question):

stage.fate = data.frame(replicate(2, sample(0:6,40,rep=TRUE)))

stage.fate$X1 = as.factor(stage.fate$X1)

stage.fate$X2 = as.factor(stage.fate$X2)

fertilities = data.frame(replicate(10,rnorm(40, .145, .045)))

df = cbind(stage.fate, fertilities)

colnames(df)[1:2]=c("stage", "fate")

prefix = "control"

suffix = seq(1:10)

fer.names = (paste(prefix ,suffix , sep="."))

colnames(df)[3:12] = c(fer.names)

Using

library(popbio)

projection.matrix(df, fertility=control.1)

returns a single transition matrix with the fertility values incorporated into the matrix.

My problem is that I would like to generate a list of matrices with the different fertility values in one go (in reality the length of my data is >=300, and the fertility columns ~100 for each of four different treatments...).

I posted this question in the stacksoverflow website (see link). I would add the answer to that threat if I get it here.

Thanks, I will appreciate your help!

-W

https://stackoverflow.com/questions/30693348/applying-popbio-projection-matrix-to-multiple-fertilities-and-generate-list-of

Similar questions and discussions