title: R dyplr: Get index of column by its name
body: <p>I need to get the index of a column from a <code>data.table</code> or <code>tbl_df</code> by it's name,</p>
  
  <p>is there an easier way than:</p>
  
  <pre><code>getColIndex &lt;- function(df, colnameTarget = "myColumnName") {
      colnames &lt;- names(traindata_subset)
      colIndex &lt;- 1
      found &lt;- FALSE
      for(colname in colnames) {
          if (colname == colnameTarget) {
              found &lt;- TRUE
              break
          }
          colIndex &lt;- colIndex + 1
      }
  
      if (found) {
          return (colIndex)
      }
      else {
          return (-1)
      }
  }
  </code></pre>
  
  <p>Especially using Hadley's <code>dplyr</code>?</p>