title: Can you use logicals within dplyr's mutate to determine whether a variable is created?
body: <p>I'm trying to create new variables within a pipe if a conditional statement is satisfied.</p>
  <p>This code provides an example of what I'm trying to achieve, based on this post: <a href="https://stackoverflow.com/questions/30604107/r-conditional-evaluation-when-using-the-pipe-operator">R Conditional evaluation when using the pipe operator %&gt;%</a>.</p>
  <pre><code>library(dplyr)
  data.frame(x = 1:3) %&gt;% 
    {if (T) mutate(., y = x * 2) else .} %&gt;%  #y is created correctly
    {if (F) mutate(., z = x * 3) else .}  #z is correctly not created since the logical is F
  #   x y
  # 1 1 2
  # 2 2 4
  # 3 3 6
  </code></pre>
  <p>I'm wondering if there's a tidier/more concise alternative which uses logicals within mutate() to determine if the variable is created? Something which looks broadly like the next block, but which produces the correct output as per the first block.</p>
  <pre><code>data.frame(x = 1:3) %&gt;% 
    mutate(y = if (T) x * 2 else ., 
           z = if (F) x * 3 else .)  #this line results in an additional 'x' column being appended, which is not the desired output
  #   x y x
  # 1 1 2 1
  # 2 2 4 2
  # 3 3 6 3  
  
  </code></pre>