Replicate elements of a list in an array in R -


i new forum , r, beforehand apologies if question not clear or don't folllow 'unwritten rules' of forum.

i doing simulation study in r loop running function several times. write results of function in list, each time loop runs function. these lists combine in array, dim(x,x,number of simulations). not sure whether right way do, looking way store x (number of simulations)-lists results in handy way in 1 variable (so can use variable var[i] <- function.. in loop).

i hope clear mean. think example code makes more clear looking for:

thaks lot hints/suggestions/answers of kind!

#these e.g. variables output function. <- matrix(na, 2,1) b <- matrix(na,2,1) beta <- matrix(na,2,1)  bp <- array(na,c(2,2,2))  #i save these variables in list (this done in function self) results <- list(a = a, b = b, beta = beta, bp = bp)  #now, create array save list each time run  #function in loop. tried this, did not succeed:  results2 <- array(results, c(1,1,10)) results3 <- array(rep(results)) 

if understand question correctly, might want use list store outputs of function fun (as example made return value of function dependent on function argument x)

fun <- function(x){   ##these e.g. variables output function.   <- matrix(x/2, 2,1)   b <- matrix(x/2,2,1)   beta <- matrix(x/2,2,1)    bp <- array(x/2,c(2,2,2))   list(a = a, b = b, beta = beta, bp = bp) } 

then use lapply wrapper for-loop in order list results (here function called 100 times):

all.results <- lapply(1:100, fun) 

you can access result of each function call all.results[[1]], all.results[[2]] etc.

alternatively, if want use for loop:

all.results <- list() (i in 1:100){   all.results[[i]] <- fun(i) } 

does help?


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -