title: Saving multiple variables inside a loop R
body: <p>I'm using this code to do the same operation over a series of data frames. Everything seems to works ok, until the moment I try to save each data frame with a different variable in two <code>.Rda</code> files.
  The problem is that I end up with the two <code>.Rda</code> files with the variables names of the data frame correctly but the content of these variables is exactly the same. It seems that is only working with the last data frame of <code>variables</code>. I think it's related with the loop for, but I don't know how to solve this.</p>
  
  <pre><code>#Previous steps, loading files...
  setwd("path_to_files")
  list.files()
  
  files &lt;- list.files(pattern = "toptable")
  
  envar &lt;- new.env()
  
  #Load all files
  for (i in files){load(i, envir = envar)}
  
  varName &lt;- ls(envar)
  
  rm(i)
  attach(envar)
  variables &lt;- as.list(envar)
  
  #From now on the function
  setwd("/path_to_save")
  
  
  
  filtering_data &lt;- function(x){
  
    #In here I'm just filtering data frames by certain values of it's columns (P.Value column and t column)
    x &lt;- as.data.frame(x)
    pval &lt;- which(x$P.Value &lt; 0.05)
    pval &lt;- x[pval,]
  
    up.pval &lt;- which(pval$t &gt; 0)
    down.pval &lt;- which(pval$t &lt; 0)
  
    up.pval &lt;- pval[up.pval,]
    down.pval &lt;- pval[down.pval,]
  
    #Modify names to use them as variable names when saving
    up_varNames &lt;- paste0(varName, ".", "up")
    down_varNames &lt;- paste0(varName, ".", "down")
  
    for (i in seq_along(up_varNames)){
      assign(up_varNames[i], up.pval)
      assign(down_varNames[i], down.pval)
    }
  
    save(list = up_varNames, file = "up.Rda" )
    save(list = down_varNames, file = "down.Rda" )
    }
  
  #function
  lapply(variables, filtering_data)
  
  
  detach(envar)
  </code></pre>