title: in R function, assign data frame object as a name? loop through function
body: <p>My question is two fold. First, given these three data frames:</p>
  
  <pre><code>df1 &lt;- data.frame(k1 = runif(6, min=0, max=100), 
               k2 = runif(6, min=0, max=100), 
               k3 = runif(6, min=0, max=100), 
               k4 = runif(6, min=0, max=100))
  df2 &lt;- data.frame(k1 = runif(6, min=0, max=100), 
                k2 = runif(6, min=0, max=100), 
                k3 = runif(6, min=0, max=100), 
                k4 = runif(6, min=0, max=100))
  df3 &lt;- data.frame(k1 = runif(6, min=0, max=100), 
                k2 = runif(6, min=0, max=100), 
                k3 = runif(6, min=0, max=100), 
                k4 = runif(6, min=0, max=100))
  </code></pre>
  
  <p>I would like to reformat and rename part of each data frame using this function:</p>
  
  <pre><code>samplelist&lt;-c("k2", "k4")
  
  draft_fxn&lt;-function(x, obj_name){
    x.selected&lt;-x[,c(samplelist)] #select columns of choice
    colnames(x.selected)[1:2]&lt;-paste(obj_name, colnames(x.selected), sep="_") #rename columns so they include original data frame name
    return(x.selected)
  }
  
  #Example run and output:
  df2_final&lt;-draft_fxn(df2, "df2")
  #output from:
  head(df2_final[1:2],)
  &gt;     df2_k2   df2_k4
  &gt;1  5.240274 53.03423
  &gt;2  5.042926 34.78974
  </code></pre>
  
  <p>First question: How can I change my function so I don't have to type in ' df2, "df2" '. In my draft_fxn code, I want to replace "obj_name" with whatever the name of the input data frame is. In my example it is "df2".</p>
  
  <p>Second question: How can I loop through all of my data frames? Perhaps, similar to this for loop?
      objs&lt;-c(df1, df2, df3)</p>
  
  <pre><code>for (file in objs){
    out&lt;-draft_fxn(file); return(out)
  } #this doesn't work though. 
  </code></pre>