title: Using R & dplyr to summarize - group_by, count, mean, sd
body: <p>Good day and greetings! This is my first post on Stack Overflow. I am fairly new to R and even newer dplyr. I have a small data set comprised of 2 columns - var1 and var2. The var1 column is comprised of num values. The var2 column is comprised of factors with 3 levels - A, B, and C. </p>
  
  <pre><code>        var1 var2
  1  1.4395244    A
  2  1.7698225    A
  3  3.5587083    A
  4  2.0705084    A
  5  2.1292877    A
  6  3.7150650    B
  7  2.4609162    B
  8  0.7349388    B
  9  1.3131471    B
  10 1.5543380    B
  11 3.2240818    C
  12 2.3598138    C
  13 2.4007715    C
  14 2.1106827    C
  15 1.4441589    C
  
  'data.frame':   15 obs. of  2 variables:
   $ var1: num  1.44 1.77 3.56 2.07 2.13 ...
   $ var2: Factor w/ 3 levels "A","B","C": 1 1 1 1 1 2 2 2 2 2 ...
  </code></pre>
  
  <p>I am trying to use dplyr to group_by var2 (A, B, and C) then count, and summarize the var1 by mean and sd. The count works but rather than provide the mean and sd for each group, I receive the overall mean and sd next to each group.</p>
  
  <p>To try to resolve the issue, I have conducted multiple internet searches. All results seem to offer a similar syntax to the one I am using. I have also read through all of the recommended posts that Stack Overflow offered prior to posting. Also, I tried restarting R and I made sure that I am not using plyr. </p>
  
  <p>Here is the code that I used to create the data set and the dplyr group_by / summarize.</p>
  
  <pre><code>library(dplyr)
  set.seed(123)
  var1 &lt;- rnorm(15, mean=2, sd=1)
  var2 &lt;- c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B",
            "C", "C", "C", "C", "C")
  df &lt;- data.frame(var1, var2)
  df
  
  df %&gt;%
    group_by(df$var2) %&gt;%
    summarize(
      count = n(),
      mean = mean(df$var1, na.rm = TRUE),
      sd = sd(df$var1, na.rm = TRUE)
    )
  </code></pre>
  
  <p>Here are the results:</p>
  
  <pre><code># A tibble: 3 x 4
    `df$var2` count  mean    sd
    &lt;fct&gt;     &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt;
  1 A             5  2.15 0.845
  2 B             5  2.15 0.845
  3 C             5  2.15 0.845
  </code></pre>
  
  <p>The count appears to work showing a count of 5 for each group. Each group is showing the overall mean and sd for the whole column rather than each group. The expected results are the count, mean, and sd for each group.</p>
  
  <p>I am sure I am overlooking something obvious but I would greatly appreciate any assistance.</p>
  
  <p>Thanks!</p>