title: Use dynamic variable names in dplyr case_when()
body: <p>I have a function in R which uses case_when:</p>
  <pre><code>myfunction &lt;- function(df, col, case_name, cntl_name) {
  
  object &lt;- df %&gt;%
      mutate(
          class = case_when(
              col == case_name ~ 1,
              col == cntl_name ~ 0,
           )
       )
  return(object)
  }
  </code></pre>
  <p>So if I have this object:</p>
  <pre><code>df &lt;- structure(list(id = c(&quot;ID1&quot;, &quot;ID2&quot;, 
  &quot;ID3&quot;, &quot;ID4&quot;, &quot;ID5&quot;
  ), phenotype = c(&quot;blue&quot;, &quot;blue&quot;, &quot;red&quot;, 
  &quot;green&quot;, &quot;red&quot;), treatment = c(&quot;treat1&quot;, &quot;treat2&quot;, 
  &quot;none&quot;, &quot;none&quot;, &quot;none&quot;), weeks_of_treatment = c(0, 0, 0, 0, 0
  )), row.names = c(&quot;ID1&quot;, &quot;ID2&quot;, 
  &quot;ID3&quot;, &quot;ID4&quot;, &quot;ID5&quot;
  ), class = &quot;data.frame&quot;)
  
  &gt; df
       id phenotype treatment weeks_of_treatment
  ID1 ID1      blue    treat1                  0
  ID2 ID2      blue    treat2                  0
  ID3 ID3       red      none                  0
  ID4 ID4     green      none                  0
  ID5 ID5       red      none                  0
  </code></pre>
  <p>And run:</p>
  <pre><code>newdf &lt;- myfunction(df, &quot;phenotype&quot;, &quot;red&quot;, &quot;blue&quot;)
  </code></pre>
  <p>It should return a dataframe that looks like this:</p>
  <pre><code>   id phenotype treatment weeks_of_treatment class
  1 ID1      blue    treat1                  0     0
  2 ID2      blue    treat2                  0     0
  3 ID3       red      none                  0     1
  4 ID4     green      none                  0    NA
  5 ID5       red      none                  0     1
  </code></pre>
  <p>But it doesn't - it returns this:</p>
  <pre><code>&gt; newdf
     id phenotype treatment weeks_of_treatment class
  1 ID1      blue    treat1                  0    NA
  2 ID2      blue    treat2                  0    NA
  3 ID3       red      none                  0    NA
  4 ID4     green      none                  0    NA
  5 ID5       red      none                  0    NA
  </code></pre>
  <p>It does not recognise the variable as <code>col</code> as the column <code>phenotype</code>.  Does anyone know how to input a dynamic variable into <code>case_when</code>?</p>
  <p>I have tried other solutions for variables in dplyr (eg, using double brackets around col <code>[[col]]</code>) but I can't find something that works.</p>