title: How to create new variable at the end of each loop iteration in R
body: <p>I am trying to create a variable that is a function of 4 other variables. I have the following code:</p>
  
  <pre><code>set.seed(123)
  iter &lt;- 1000
  group &lt;- c('A','B','C','D','E','F')
  for (i in group) {
    df &lt;- df1[df1$group == i,]
    x_ &lt;- vector(mode="numeric", length=1000)
    assign(eval(paste0("X_", i)), globalenv())   #This is the issue
  
    a &lt;- rnorm(iter, mean=df$a, sd=df$sea)
    b &lt;- rnorm(iter, mean=df$b, sd=df$seb)
    c &lt;- rnorm(iter, mean=df$c, sd=df$sec)
    z &lt;- rnorm(iter, mean=df$zbar, sd=df$se_z)
  
    X_[i] = (a + c*(z-df$zbar))/(-b)
  }
  </code></pre>
  
  <p>I am unable to create a unique group-specific variable (e.g. X_A, X_B, ...) and I am unsure why the -assign( )- function is not working properly. The dataframe <code>df1</code> has 6 rows (one for each group) and then the number of columns is equal to the number of variables plus a string variable for group. I am not trying to append this new variables <code>X_[i]</code> to the dataset I am just trying to place it in the global environment. I believe the issue lies in my assigning the placement of the variable, but it isn't generating a numeric variable X.</p>
  
  <p><code>df1</code> is a dataframe with 6 observations of 9 variables containing <code>a, sea, b, seb, c, sec, zbar, se_z</code>. These are just the means and standard deviations of a, b, c, and z, respectively. The 9th variable is <code>group</code> which contains A, B, ..., F. When I use the code <code>df &lt;-df1[df1$group == i,]</code> I am trying to create a unique <code>X</code> variable for each group entity.</p>