Hi, I'm trying to write a code (see below) to randomly resample measurements of one variable (say here the variable "counts" in the data frame "dat") with different resampled subsample sizes. The code works fine for a single resampled subsample size (in the code below = 10). I then tried to generalize this by writing a function with a loop, where in each loop the function should do the calculations with increasing subsample size (say for j in 1:100). Problem is, I can't tell R to write the results of each of these loops. As a result, the function always keeps the results of the last loop (with j=100). Many thanks in advance for any suggestion. # Creating data frame with "counts" and "depths" test <- matrix(rep(1, 1000)) depths <- matrix(1:1000) dat <- cbind(test, depths) rm(depths, test) colnames(dat) <- c("counts", "depths") dat <- as.data.frame(dat) ### Easy code to resample 999 times 10 "counts" from dat require(MASS) # Makes sums and var based on 999 resampled counts and stores results in a matrix # with sums and vars in 999 rows and 2 columns - THIS WORKS resamples10 <- lapply(1:999, function(i) sample(dat$counts, 10, replace=T)) r10.stat <- cbind(sapply(resamples10, sum), sapply(resamples10, var), sapply(resamples10, mean)) colnames(r10.stat) <- c("r10.sum", "r10.var", "r10.mean") ### NEED to generalize this for different sample sizes # Creates the resample function with arguments "data", stat. # by default, num = size of resampled subsample varies for j in 1:100 # CAN'T tell him to store all sums of different sample sizes in one data.frame require(MASS) b.stat <- function(data, stat) { for (j in 1:100){ rj.repl <- lapply(1:999, function(i) sample(data, j, replace=T)) rj.stat <- sapply(rj.repl, stat) rj.stderr <- sqrt(var(rj.stat)) list(std.err=rj.stderr, resamples=rj.repl, stats=rj.stat) } b.sum <- cbind([, rj.stat]) } b.sum <- b.stat(dat$counts, sum) -- View this message in context: http://r.789695.n4.nabble.com/repeat-resampling-with-different-subsample-sizes-tp4655927.html Sent from the R help mailing list archive at Nabble.com.