For loop use in R to use multiple files in a directory
<p>I have multiple files coded by id number in a directory. each file has four variables. One of the variable has same id number as that of file. I need to run a function mean for one of the other three variables, using each of the file. </p>

<p>I am a beginner learner. So far I have seen from this website and others, that how to use for and if loops to use data from a single file. I have tried many combinations. One of this is as follows. However, I am not getting the desired output. Can you guide?</p>

<pre><code>pollutantmean&lt;- function (directory, pollutant, id = 1:332){


    directory  = "~/specdata" 
    pollutant = "sulfate"
    for (i in 1:332){         
        pollutantmean (directory [i])
        options(expressions=1000) 
        }
        means
}
</code></pre>

<p><code>id</code> is the idnumber of each file contained in a directory "<code>specdata</code>" at my working directory and pollutant is the variable that will be used by either of two variables- sulfate and nitrate  in datasets</p>

<p>However, when running</p>

<pre><code>pollutantmean()    
</code></pre>

<p>I get</p>

<blockquote>
  <p>Error: evaluation nested too deeply: infinite recursion /
  options(expressions=)? Error during wrapup: evaluation nested too
  deeply: infinite recursion / options(expressions=)?</p>
</blockquote>