[R] Help vectorising a function

From: hadley wickham <h.wickham_at_gmail.com>
Date: Tue 26 Jul 2005 - 00:22:29 EST


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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jul 26 00:31:33 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:34:01 EST