title: Julia - dataframe - How to force cast as a vector/multiple line value a scalar results using by()
body: <p>Long and winding road, exploring DataFrames.jl and one more question.</p>
  
  <p>I am trying to force cast a function returning a scalar to map back to the entire vector.
  I am not sure this is entirely clear so let me exemplify : </p>
  
  <p>Lets say I am running the following  (and now canonical) example</p>
  
  <pre><code>df = DataFrame(grp = rand(["a","b"], 100), x= rand(100), y = rand(100), z=rand(100));
  by(df, :grp,result= (:x) =&gt; (x) -&gt; sum(x))
  </code></pre>
  
  <pre><code>2×2 DataFrame
  │ Row │ grp    │ result  │
  │     │ String │ Float64 │
  ├─────┼────────┼─────────┤
  │ 1   │ b      │ 30.431  │
  │ 2   │ a      │ 19.9667 │
  </code></pre>
  
  <p>I get the sum of x, for each of the group. All good. But for some reason I want to have this sum of each group casted for each of the line in the dataset</p>
  
  <p>I found the following hack </p>
  
  <pre><code>by(df, :grp,result= (:x) =&gt; (x) -&gt; x.-x.+sum(x))
  </code></pre>
  
  <p>Giving exactly what I expect</p>
  
  <pre><code>│ Row │ grp    │ result  │
  │     │ String │ Float64 │
  ├─────┼────────┼─────────┤
  │ 1   │ b      │ 30.431  │
  │ 2   │ b      │ 30.431  │
  │ 3   │ b      │ 30.431  │
  ⋮
  │ 98  │ a      │ 19.9667 │
  │ 99  │ a      │ 19.9667 │
  │ 100 │ a      │ 19.9667 │
  </code></pre>
  
  <p>Now, I essentially force a vector aspect in the function to make it happen, (I also do it in the most inelegant way). </p>
  
  <p><strong>My question is : Is there with by() a correct/nicer way to force a function returning a scalar to map back to the original vector ?</strong></p>
  
  <p>In my opinion this would be very helpful and also help to address the following situations. </p>
  
  <pre><code>by(df, :grp,result= (:x) =&gt; (x) -&gt; sum(x),result2= (:x) =&gt; (x) -&gt; mean(x)) # works returns 2 lines 
  by(df, :grp,result= (:x) =&gt; (x) -&gt; sum(x),result2= (:x) =&gt; (x) -&gt; x.-5) # doesnt work (scalar vs vector)
  by(df, :grp,result= (:x) =&gt; (x) -&gt; x.-x.+sum(x),result2= (:x) =&gt; (x) -&gt; x.-5) # works returns 100 lines
  </code></pre>
  
  <p>I am sure I am missing something any help would be great.</p>