title: Create a numeric variable in a dataframe based upon mean of another variable and a factor
body: <p>Ok so I have what is very likely a simple question albeit one that I have found hard to pose (that is likely the root of the problem).</p>
  
  <p>If I have the following example data:</p>
  
  <pre><code>    V1 &lt;- c(1,1,1,1,1,2,2,2,2,2)
      factor &lt;- factor(V1)
      V2 &lt;- c(1,2,3,4,5,6,7,8,9,10)
      V3 &lt;- c(10,20,30,40,50,60,70,80,90,100)
      test &lt;- data.frame(factor,V2,V3)
  </code></pre>
  
  <p>How might I go about generating another variable, lets say V4, that is the mean of V3 for each level of factor? I can get the mean values using for example tapply:</p>
  
  <pre><code>    tapply(test$V3, test$factor, FUN=mean)
  </code></pre>
  
  <p>which in this case would result in 30 and 80 respectively but I want this to form a repeating variable the length of the relevant factor level as follows:</p>
  
  <pre><code>      factor V2  V3 v4
     1       1  1  10 30
     2       1  2  20 30
     3       1  3  30 30
     4       1  4  40 30
     5       1  5  50 30
     6       2  6  60 80
     7       2  7  70 80
     8       2  8  80 80
     9       2  9  90 80
     10      2 10 100 80
  </code></pre>
  
  <p>Any suggestions and solutions would be welcome along with how better to phrase the question.</p>