title: subset data frame returns factor with level instead of single value
body: <p>I have a data frame that contains 2 columns,  <code>filename</code> and <code>monitorid</code>.</p>
  
  <pre><code>  filename monitorid
  1  001.csv         1
  2  002.csv         2
  3  003.csv         3
  4  004.csv         4
  5  005.csv         5
  6  006.csv         6
  </code></pre>
  
  <p>I am trying to subset in order to select the <code>filename</code> for a given <code>monitorid</code></p>
  
  <pre><code>filename &lt;- files[files$monitorid==3,1]
  </code></pre>
  
  <p>I expected this to return "003.csv"</p>
  
  <p>Instread it returns</p>
  
  <pre><code>[1] 003.csv
  6 Levels: 001.csv 002.csv 003.csv 004.csv 005.csv 006.csv
  </code></pre>
  
  <p>However </p>
  
  <pre><code>filename &lt;- files[files$monitorid==3,2] returns 
  [1] 3
  </code></pre>
  
  <p>as expected</p>
  
  <p>I do not understand why choosing column 1 returns a factor with multiple levels while column 2 returns a single value.</p>
  
  <p>Any ideas would be greatly appreciated.</p>
  
  <hr>
  
  <p>@KenM 
   This is the function I used to read the files names</p>
  
  <pre><code>getfileinfo &lt;- function (directory){
  ## Reads file names into filenames variable        
          filenames &lt;- list.files (path = directory)
  ## assigns monitorids to each file name
          monitorid &lt;- as.numeric(substr(filenames,1,3))
  ##combines filenames and monitorid into data frame, files
          files &lt;- data.frame(filenames, monitorid)
          names(files) &lt;- c("filename","monitorid")
          return(files)
  
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Solution</strong></p>
  
  <p>Here's is the ouput from each line</p>
  
  <pre><code>    filenames &lt;- list.files (path = directory)
      class(filenames)
  [1] "character"
      monitorid &lt;- as.numeric(substr(filenames,1,3))
      class(monitorid)
  [1] "numeric"
      files &lt;- data.frame(filenames, monitorid)
      sapply (files, class)
  filenames monitorid 
   "factor" "numeric" 
  </code></pre>
  
  <p>As noted by both KenM and BeginneR when combined into a data frame the character vector filenames becomes a column of data class factor</p>
  
  <p><strong>Corrected code</strong></p>
  
  <pre><code>files &lt;- data.frame(filenames, monitorid, stringsAsFactors = FALSE)
  sapply (files, class)
    filenames   monitorid 
  "character"   "numeric" 
  </code></pre>