title: Trying to use dplyr to group_by and apply scale()
body: <p>Trying to use <code>dplyr</code> to <code>group_by</code> the <code>stud_ID</code> variable in the following data frame, as in <a href="https://stackoverflow.com/questions/26950045/group-by-and-scale-normalize-a-column-in-r">this SO question</a>:</p>
  
  <pre><code>&gt; str(df)
  'data.frame':   4136 obs. of  4 variables:
   $ stud_ID         : chr  "ABB112292" "ABB112292" "ABB112292" "ABB112292" ...
   $ behavioral_scale: num  3.5 4 3.5 3 3.5 2 NA NA 1 2 ...
   $ cognitive_scale : num  3.5 3 3 3 3.5 2 NA NA 1 1 ...
   $ affective_scale : num  2.5 3.5 3 3 2.5 2 NA NA 1 1.5 ...
  </code></pre>
  
  <p>I tried the following to obtain scale scores by student (rather than scale scores for observations across all students):</p>
  
  <pre><code>scaled_data &lt;- 
            df %&gt;%
                group_by(stud_ID) %&gt;%
                    mutate(behavioral_scale_ind = scale(behavioral_scale),
                           cognitive_scale_ind = scale(cognitive_scale),
                           affective_scale_ind = scale(affective_scale))
  </code></pre>
  
  <p>Here is the result:</p>
  
  <pre><code>&gt; str(scaled_data)
  Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 4136 obs. of  7 variables:
   $ stud_ID             : chr  "ABB112292" "ABB112292" "ABB112292" "ABB112292" ...
   $ behavioral_scale    : num  3.5 4 3.5 3 3.5 2 NA NA 1 2 ...
   $ cognitive_scale     : num  3.5 3 3 3 3.5 2 NA NA 1 1 ...
   $ affective_scale     : num  2.5 3.5 3 3 2.5 2 NA NA 1 1.5 ...
   $ behavioral_scale_ind: num [1:12, 1] 0.64 1.174 0.64 0.107 0.64 ...
    ..- attr(*, "scaled:center")= num 2.9
    ..- attr(*, "scaled:scale")= num 0.937
   $ cognitive_scale_ind : num [1:12, 1] 1.17 0.64 0.64 0.64 1.17 ...
    ..- attr(*, "scaled:center")= num 2.4
    ..- attr(*, "scaled:scale")= num 0.937
   $ affective_scale_ind : num [1:12, 1] 0 1.28 0.64 0.64 0 ...
    ..- attr(*, "scaled:center")= num 2.5
    ..- attr(*, "scaled:scale")= num 0.782
  </code></pre>
  
  <p>The three scaled variables (<code>behavioral_scale</code>, <code>cognitive_scale</code>, and <code>affective_scale</code>) have only 12 observations - the same number of observations for the first student, <code>ABB112292</code>. </p>
  
  <p>What's going on here? How can I obtain scaled scores by individual?</p>