Data manipulation in columns (with apply?)
Does this start to do what you want?
x <- "NUM sim N
+ 1 1 466 + 1 2 450 + 1 3 473 + 1 4 531 + 1 5 515 + 1 6 502 + 1 7 471 + 1 8 460 + 1 9 458 + 1 10 434 + 2 1 289 + 2 2 356 + 2 3 387 + 2 4 440 + 2 5 457 + 2 6 466 + 2 7 467 + 2 8 449 + 2 9 387 + 2 10 394 + 3 1 367 + 3 2 400 + 3 3 476 + 3 4 508 + 3 5 478 + 3 6 501 + 3 7 513 + 3 8 505 + 3 9 492 + 3 10 465"
a <- read.table(textConnection(x), header=T) lambda <- by(a, a$NUM, function(x) x$N[-1] / x$N[-length(x$N)]) lambda
a$NUM: 1 [1] 0.9656652 1.0511111 1.1226216 0.9698682 0.9747573 0.9382470 0.9766454 0.9956522 0.9475983 ------------------------------------------------------------------------------ a$NUM: 2 [1] 1.2318339 1.0870787 1.1369509 1.0386364 1.0196937 1.0021459 0.9614561 0.8619154 1.0180879 ------------------------------------------------------------------------------ a$NUM: 3 [1] 1.0899183 1.1900000 1.0672269 0.9409449 1.0481172 1.0239521 0.9844055 0.9742574 0.9451220
# sum of lambdas sapply(lambda, sum)
1 2 3 8.942166 9.357799 9.263944
# mean sapply(lambda, mean)
1 2 3 0.993574 1.039755 1.029327
# sd sapply(lambda, sd)
1 2 3 0.05822850 0.10525335 0.08004527
On 10/10/06, Bret Collier <bret at tamu.edu> wrote:
R Users, I have written a small simulation model in R which outputs a datafile consisting of ending population sizes for each simulation run (year). The data (see short data example below) is labeled by NUM (simulation run), sim (year) and N (yearly count). After searching the help files and coming up empty (probably because I used the wrong terms) I am appealing for some help for working with the output dataset. What I want to do is for each of the i simulation runs (NUM) I want to 1) take N(t+1)/N(t)=lambda(t) for each year (where in the below example t=1,...,10--total years of the simulation) 2) Sum lambda(t) and divide by t (e.g., output both the mean/se of lambda for each simulation run) 3) Take the mean of the mean(lambda's) (and associated stddev, min, max) over all NUM I think I have to write a function for use within an apply statement, but I am not quite there yet on the learning curve so most of my recent attempts in R have been useful learning experiences of what not to do... Any suggestions/direction is greatly appreciated. Bret Collier TX A&M NUM sim N 1 1 466 1 2 450 1 3 473 1 4 531 1 5 515 1 6 502 1 7 471 1 8 460 1 9 458 1 10 434 2 1 289 2 2 356 2 3 387 2 4 440 2 5 457 2 6 466 2 7 467 2 8 449 2 9 387 2 10 394 3 1 367 3 2 400 3 3 476 3 4 508 3 5 478 3 6 501 3 7 513 3 8 505 3 9 492 3 10 465 platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 3.0 year 2006 month 04 day 24 svn rev 37909 language R version.string Version 2.3.0 (2006-04-24) (yeah, I need to update)
______________________________________________ R-help at 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 and provide commented, minimal, self-contained, reproducible code.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?