[R] Using loop for a random vector (Montecarlo method)

mlell08 mlell08 at gmail.com
Tue Jan 29 22:38:43 CET 2013


> # This assumes you have a parameter list which looks like this:
> # l: list(n = c(5, 10, 20),
> #         a = c(0.1, 0.2, 0.3),
> #         E = c(5,10),
> #         L = c(0.01, 0.025, 1))  etc...
> #
> #
> # indVec:c(n= 1, a=1, E=2, L=1)
> # The function selects the approproate sub-list by the name
> getParam <- function(indexVec,l){
> 	n <- names(indexVec)    # get parameter names
> 	ret <- c()        # create list to contain the parameter values
> 	for (i in 1:length(indVec)){
> 		para <- l[[ n[i] ]]    # get the right parameter
> 		# and choose the value specified by indexVec
>  		#Append the value to the list to be returned
> 		ret <- c(ret, para[ indVec[i] ])
> 	}	
> 	return(ret)
> }

Oops, typo: make indexVec out of indVec :-/

-- 
GnuPG Key: 0x7340821E



More information about the R-help mailing list