title: dplyr: using filter, group_by, from within mutate command
body: <p>I want to add a column to the data table that contains each value of y divided by the mean of the corresponding condition in x (1 or 2) where x2 = 1. For the following data where x = 1 y should be divided by 1.4 and where x=2 y should be divided by 1.</p>
  
  <pre><code>dt1 &lt;- data.table(x=c("1","1","1","1","1","1","1","1","1","1","2","2","2","2","2","2","2","2","2","2"),
         x2=c("1","1","2","2","2","2","3","3","3","3","1","1","2","2","2","2","3","3","3","3"), 
         y=c(1.41,1.39,1.9,2.1,0.9,1.1,3.1,2.9,3.9,4.1,0.9,1.1,1.9,2.1,0.9,1.1,3.1,2.9,3.9,4.1))
  </code></pre>
  
  <p>I can write the mean of x * x2 = 1 to a new file. </p>
  
  <pre><code>mean &lt;- dt1 %&gt;% filter(x2==1) %&gt;% group_by(x) %&gt;% summarise(mean(y))
  </code></pre>
  
  <p>but I can't work out how to mesh instruct the commands to call the correct value.
      dt1 %>% mutate(z = y/a reference to 'mean')</p>
  
  <p>I thought creating a new column filled with the values that I want to divide by but once again I can't work out how to call the grouping criteria from within the command.</p>
  
  <pre><code>t &lt;- dt1 %&gt;% mutate(T=ifelse(x==1,(filter(x2==1) %&gt;% group_by(x=1) %&gt;%
       summarise(mean(y))),ifelse(x==1,(filter(x2==2) %&gt;% group_by(x=2) %&gt;% 
       summarise(mean(y))),NA)
  </code></pre>
  
  <p>I'm not fixed on only using dplyr but I've been using it a lot recently. I'm open to the simplest solution. </p>