title: how to use lapply in R
body: <p>I am trying to use <code>lapply</code> so that I can apply a custom function on all elements of a vector in R. I am trying to avoid using <code>for</code> loop here .</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>writeToHDFS &lt;- function(fileName){
    print(fileName)
    hdfs.init()
   modelfile &lt;- hdfs.file(fileName, "w")
    hdfs.write(gaDataUser, modelfile)
   hdfs.close(modelfile)
  }
  
  fileNames &lt;- c("gaDataUser", "gaDataSession", "gaDataSources","gaDataEventTracking", "gaDataEcommerce", "gaDataEcommerce2", "gaDataEcommerce3", "gaDataEcommerce4")
  
  
  lapply(fileNames,writeToHDFS(x))
  </code></pre>
  
  <p>I have variables with the names mentioned in the character vector <code>fileNames</code>.</p>
  
  <p><strong>What I need to know:</strong></p>
  
  <ol>
  <li><p>How to pass each string from the vector <code>fileNames</code> to function <code>writeToHDFS</code> since I would like this function to be executed for every element in the vector.</p></li>
  <li><p>How to use string name to access variables of that name in the function. 
  For example:</p></li>
  </ol>
  
  <p>At this line,I have to access variable with name same as string passed to <code>fileName</code> variable in the function.</p>
  
  <pre><code>hdfs.write(variableWithData, modelfile)
  </code></pre>
  
  <p>3. Can I pass the variable <code>fileName</code> to  </p>
  
  <pre><code>modelfile &lt;- hdfs.file(fileName, "w")
  </code></pre>
  
  <p>instead of passing a string for file name ?</p>