title: Error in drop && !has.j : invalid 'x' type in 'x && y’ when using sum(complete.cases) Windows7 R3.2.1
body: <p>I am very new to programming, both in R and in general. </p>
  
  <p>Here is my goal for writing this script: </p>
  
  <ol>
  <li><p>I have 332 csv files. I want to, “Write a function that 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.”</p></li>
  <li><p>The outline of the function is as follows:</p>
  
  <p>complete &lt;- function(directory, id = 1:332) {
          ## 'directory' is a character vector of length 1 indicating
          ## the location of the CSV files</p>
  
  <pre><code>    ## 'id' is an integer vector indicating the monitor ID numbers
      ## to be used
  
      ## Return a data frame of the form:
      ## id nobs
      ## 1  117
      ## 2  1041
      ## ...
      ## where 'id' is the monitor ID number and 'nobs' is the
      ## number of complete cases
  </code></pre>
  
  <p>}</p></li>
  </ol>
  
  <p>Example output would look like this: </p>
  
  <pre><code>source("complete.R")
  complete("specdata", 1)
  ##   id nobs
  ## 1  1  117
  
  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>
  
  <ol start="4">
  <li>My script so far looks like this: </li>
  </ol>
  
  <p>setwd("C:/users/beachlb/Desktop/R_Programming/specdata") #this is the local directory on my computer where all 332 csv files are stored</p>
  
  <pre><code>&gt;complete &lt;- function(directory, id = 1:332) {
  
  &gt;files_list &lt;- list.files(directory, full.names=TRUE) #creates a list of files from within the specified directory
  
  &gt;dat &lt;- data.frame() #creates an empty data frame that we can use to add data to
  
  &gt;for (i in id) {
  
  &gt;dat &lt;- rbind(dat, read.csv(files_list[i]))  #loops through the 332 csv files, rbinding them together into one data frame called dat
    }
  
  &gt;dat$nobs &lt;- sum(complete.cases(dat)) #add the column nobs to dat, populated with number of rows of complete cases in the dataframe
  
  &gt;dat_subset &lt;- dat[which(dat[, "ID"] %in% id),] #subsets dat so that only the desired cases are included in output when function is run
  
  &gt;dat_subset[, "ID", "nobs"] #prints all rows of the desired data frame for the named columns}
  </code></pre>
  
  <ol>
  <li><p>When I run my function as is, I get this error, “Error in drop &amp;&amp; !has.j : invalid 'x' type in 'x &amp;&amp; y’. I am not sure what is throwing me that error. I would appreciate any advice on what could be causing this error and how I can work to resolve it. Pointing me to literature I could read to study this and/or tutorials that would help me strengthen the coding skills needed to avoid this error would also be appreciated. </p></li>
  <li><p><strong>Preface: I am not sure if I should ask this question on a separate thread.</strong> Right now, my function is written to populate the total number of complete cases for all rows (for all 332 files), instead of specifically calculating the number of complete cases for a given monitor id and putting that into the column nobs for that ID only. (Note that each file is named after the monitor id and contains only cases from that monitor, such that 001.csv = output from monitor 1, 002.csv = output from monitor 2). Therefore, I am hoping for someone to help point me to a resource for how to subset dat so that when the nobs column populates, each row in the nobs column gives the number of complete cases for each id number. </p></li>
  </ol>