[R] Help vectorising a function
hadley wickham
h.wickham at gmail.com
Mon Jul 25 16:22:29 CEST 2005
M <- function(m, s, init = 0) {
A <- nrow(m); T <- ncol(m)
M <- matrix(init, nrow = A, ncol = T)
for(a in 1:(A-1)) {
M[a+1, 2:T] <- (s[a] * (M[a, ] + m[a, ]))[1:(T-1)]
}
M
}
This is from a mark-recapture study where M is an estimate of the
number of marked fish in each age class (A) over each year (T). s is
a vector of age dependent survival probabilities and m is a matrix
containing the number of fish marked for the first time (in each age
class and year).
I'm pretty sure there's a much better way of doing this - but I can't
see it. I'm not looking for the exact code to solve the problem -
just a better way of attacking it. Any hints would be much
appreciated!
Hadley
More information about the R-help
mailing list