R- Error in `[.data.frame`(rows, ID == 1) : object 'ID' not found
<p>Following function reads a directory full of files and reports the number of completely observed cases in each data file. The function should return a data frame where the first column is the name of the file and the second column is the number of complete cases. A prototype of this function follow</p>

<pre><code>complete &lt;- function(directory,id = 1:332) {
        ##set up directory
        file_names &lt;-
            dir(paste("C:/Users/Bruce/Desktop",directory,sep = "/"))
        ## combine the 332 files into data frame in the directory
        data_frame &lt;- do.call(rbind, lapply(file_names, read.csv))
        ## complete case
        good &lt;- complete.cases(data_frame)

        rows &lt;- data_frame[good,]
        ##structure of data_frame
        str(rows)


        nobs = vector(, 400)
        for(i in id){
            nobs &lt;- nrow(rows[which(ID == 1),])
        }

        cbind(id, nobs)

    }

    complete("specdata")
</code></pre>

<p>Errors</p>

<pre><code> &gt; complete("specdata")
    'data.frame':   111802 obs. of  4 variables:
     $ Date   : Factor w/ 4018 levels "2003-01-01","2003-01-02",..: 279 285 291 297 303 315 321 327 333 339 ...
     $ sulfate: num  7.21 5.99 4.68 3.47 2.42 1.43 2.76 3.41 1.3 3.15 ...
     $ nitrate: num  0.651 0.428 1.04 0.363 0.507 0.474 0.425 0.964 0.491 0.669 ...
     $ ID     : int  1 1 1 1 1 1 1 1 1 1 ...
     Show Traceback

     Rerun with Debug
     Error in `[.data.frame`(rows, ID == 1) : object 'ID' not found 
</code></pre>

<p>The Result should be like:</p>

<pre><code>complete("specdata", c(2, 4, 8, 10, 12))
##   id nobs
## 1  2 1041
## 2  4  474
## 3  8  192
## 4 10  148
## 5 12   96
</code></pre>