title: R: Transpose the a results table and add column headers
body: <p><strong>Setting the scene:</strong></p>
  
  <p>So I have a directory with 50 .csv files in it.</p>
  
  <p>All files have unique names e.g. 1.csv 2.csv ...</p>
  
  <p>The contents of each may vary in the number of rows but always have 4 columns</p>
  
  <p>The column headers are: </p>
  
  <ul>
  <li>Date</li>
  <li>Result 1 </li>
  <li>Result 2</li>
  <li>ID</li>
  </ul>
  
  <p>I want them all to be merged together into one dataframe (mydf) and then I'd like to ignore any rows where there is an NA value.</p>
  
  <p>So that I can count how many complete instances of an "ID" there were. By calling for example;</p>
  
  <ul>
  <li>myfunc("my_files", 1)  </li>
  <li>myfunc("my_files", c(2,4,6))</li>
  </ul>
  
  <p><strong>My code so far:</strong></p>
  
  <pre><code>myfunc &lt;- function(directory, id = 1:50) {
          files_list &lt;- list.files(directory, full.names=T)
          mydf &lt;- data.frame()
          for (i in 1:50) {
                  mydf &lt;- rbind(mydf, read.csv(files_list[i]))
          }
          mydf_subset &lt;- mydf[which(mydf[, "ID"] %in% id),]
          mydf_subna &lt;- na.omit(mydf_subset)
          table(mydf_subna$ID)
  }
  </code></pre>
  
  <p><strong>My issues and where I need help:</strong></p>
  
  <p>My results come out this way</p>
  
  <pre><code>2   4    6   
  200 400  600
  </code></pre>
  
  <p>and I'd like to transpose them to be like this. I'm not sure if calling a table is right or should I call it as.matrix perhaps?</p>
  
  <pre><code>2 100
  4 400
  8 600
  </code></pre>
  
  <p>I'd also like to have either the headers from the original files or assign new ones</p>
  
  <pre><code>ID Count
  2  100
  4  400
  8  600
  </code></pre>
  
  <p>Any and all advice is welcome</p>
  
  <p><em>Matt</em></p>
  
  <p><strong>Additional update</strong></p>
  
  <p>I tried amending to incorperate some of the helpful comments below, so I also have a set of code that looks like this;</p>
  
  <pre><code>myfunc &lt;- function(directory, id = 1:50) {
          files_list &lt;- list.files(directory, full.names=T)
          mydf &lt;- data.frame()
          for (i in 1:50) {
                  mydf &lt;- rbind(mydf, read.csv(files_list[i]))
          }
          mydf_subset &lt;- mydf[which(mydf[, "ID"] %in% id),]
          mydf_subna &lt;- na.omit(mydf_subset)
          result &lt;- data.frame(mydf_subna$ID)
          transposed_result &lt;- t(result)
          colnames(transposed_result) &lt;- c("ID","Count")
  }
  </code></pre>
  
  <p>which I try to call with this:</p>
  
  <pre><code>myfunc("myfiles", 1)
  myfunc("myfiles", c(2, 4, 6))
  </code></pre>
  
  <p>but I get this error</p>
  
  <pre><code>&gt; myfunc("myfiles", c(2, 4, 6))
  Error in `colnames&lt;-`(`*tmp*`, value = c("ID", "Count")) : 
    length of 'dimnames' [2] not equal to array extent
  </code></pre>
  
  <p>I wonder if perhaps I'm not creating this data.frame correctly and should be using a cbind or not summing the rows by ID maybe?</p>