[R] reference generated var name in loop

Evan Cooch
2023-05-26T14:49:18Z

Greetings --

I'm trying to write some code that basically does the following:

1\ user inputs a list of parameter names, and parameter values.

2\ code parses this list, extracting parameter names and values separately

3\ loops over the number of parameters, and for each parameter,
generates something (say, rnorm) based on the value corresponding to
that parameter.

4\ assigns a new name to the vector of rnorm data, where the name is a
permutation of the original parameter name. In the example (below) I
simply past "_r" to the original parameter name (to indicate, say, its
some rnorm values associated that that variable).

5\the 'I'm stumped' part -- output the newly named vector into
something
(say, data.frame), which requires being able to reference the newly
named vector, which is what can't figure out how to accomplish.

Here is a MWE of sorts -- the starting list of parameter names and
values is in a list I call parms.

 ?parms <- c(a=4,b=6,c=11) # list of parms user could pass to a function

 ?nvars <- names(parms)? # extract parameter names

 ?vals <- sub("\\=.*", "", parms) # extract parameter
values

 ?n <- length(parms) # determine number of parameters

 ?ranvec <- data.frame() # to store stuff from loop

 ?for (i in 1:n) {
 ?? pn <- nvars[i]; # get name of parm variable
 ?? rn <- rnorm(5,as.numeric(vals[i]),0.25) # generate rnorm based on
parm value
 ?? assign(paste(pn, "_r", sep=""),rn) # creates a_r, b_r,
c_r...
 ?? ** here is where I want to output to ranvec, but I can't see how to
do it since the loop doesn't know the name of the new var created in
preceding step...**
 ?}


In the end, I want to create a data frame containing rnorm deviates for
each of a set of user-specified variables, which I can reference in some
fashion using a permuted version of the original variable name.

Suggestions?

Many thanks...