title: R: How to retrieve a column name of a data frame
body: <p>I am trying to extract the colnames of a data frame, based on the values in the cells.  My data is a series of a couple hundred categories, with a simple binary 0 or 1 in the cells to indicate which column name I want in my new df.  </p>
  
  <p>To illustrate my point:</p>
  
  <pre><code>year cat1 cat2 cat3 ... catN
  2000  0    0    1        0
  2001  1    0    0        0
  2002  0    0    0        1
  ....
  2018  0    1    0        0
  </code></pre>
  
  <p>I am trying to get a df like:</p>
  
  <pre><code>year category 
  2000  cat3
  2001  cat1  
  2002  catN  
  ....
  2018  cat2  
  </code></pre>
  
  <p>My code: </p>
  
  <pre><code>newdf &lt;- as.data.frame(colnames(mydf)[which(mydf == "1", arr.ind = TRUE)[2]])
  </code></pre>
  
  <p>But alas this only returns one category name!</p>
  
  <p>Any help would be greatly appreciated!</p>