title: Variable results with dplyr summarise, depending on output variable naming
body: <p>I'm using the <code>dplyr</code> package (<code>dplyr</code> 0.4.3; R 3.2.3) for basic summary of grouped data (<code>summarise</code>), but get inconsistent results (NaN for 'sd', and incorrect count for 'N"). Changing the "name" of the output has variable effects (examples below). </p>
  
  <p><strong>Summary of results so far:</strong></p>
  
  <ul>
  <li><code>plyr</code> package is not loaded, which I know could cause problems with <code>dplyr</code>if loaded first.</li>
  <li>Same results obtained with or without NA data (not shown).</li>
  <li>Problem can be fixed by using camelCase variable naming (not shown) or by using an output variable without non-alphanumeric separator in name.</li>
  <li>Valid results still obtained depending on the combinations of "." or "_" in output col names.</li>
  </ul>
  
  <p><strong>Question:</strong> Although this problem can be worked around, am I violating a basic variable naming rule that I'm violating, or is there a program issue that needs to be addressed? I've seen other questions with variable behavior with summarise, but not quite this. </p>
  
  <p>thanks, Matt</p>
  
  <p><strong>Example Data</strong>: </p>
  
  <pre><code>library(dplyr)
  df&lt;-data_frame(id=c(1,1,1,2,2,2,3,3,3),
         time=rep(1:3, 3),
         glucose=c(90,150, 200,
                   100,150,200,
                   80,100,150))
  </code></pre>
  
  <p>Example: sd gives NaN and inaccurate n</p>
  
  <pre><code>df %&gt;% group_by(time) %&gt;%
    summarise(glucose=mean(glucose, na.rm=TRUE),
          glucose.sd=sd(glucose, na.rm=TRUE),
          n=sum(!is.na(glucose)))
  
     time  glucose glucose.sd     n
    (int)    (dbl)      (dbl) (int)
  1     1  90.0000        NaN     1
  2     2 133.3333        NaN     1
  3     3 183.3333        NaN     1
  </code></pre>
  
  <p>I wondered if it was an issue with using either "." in name, 
  or using the same name as in the dataframe. Removing existing df col names from the output fixes this </p>
  
  <pre><code>df %&gt;% group_by(time) %&gt;%
    summarise(avg=mean(glucose, na.rm=TRUE),
          stdv=sd(glucose, na.rm=TRUE),
          n=sum(!is.na(glucose)))
  
     time      avg     stdv     n
    (int)    (dbl)    (dbl) (int)
  1     1  90.0000 10.00000     3
  2     2 133.3333 28.86751     3
  3     3 183.3333 28.86751     3
  </code></pre>
  
  <p>Removing the "glucose" summary fixes it too even though "glucose.sd" is left
  Example: after removing "glucose", result is OK</p>
  
  <pre><code>df %&gt;% group_by(time) %&gt;%
    summarise(glucose.sd=sd(glucose, na.rm=TRUE),
          n=sum(!is.na(glucose)))
  
     time glucose.sd     n
    (int)      (dbl) (int)
  1     1   10.00000     3
  2     2   28.86751     3
  3     3   28.86751     3
  </code></pre>
  
  <p>If I add "glucose.mean" for first summary it works fine</p>
  
  <pre><code>df %&gt;% group_by(time) %&gt;%
    summarise(glucose.mean=mean(glucose, na.rm=TRUE),
              glucose.sd=sd(glucose, na.rm=TRUE),
              n=sum(!is.na(glucose)))
  
     time glucose.mean glucose.sd     n
    (int)        (dbl)      (dbl) (int)
  1     1      90.0000   10.00000     3
  2     2     133.3333   28.86751     3
  3     3     183.3333   28.86751     3
  </code></pre>
  
  <p>Same error when using variable name without "."
  So it's not just an issue with using "." in name</p>
  
  <pre><code>df %&gt;% group_by(time) %&gt;%
    summarise(glucose=mean(glucose, na.rm=TRUE),
          glucose_sd=sd(glucose, na.rm=TRUE),
          n=sum(!is.na(glucose)))
  
     time  glucose glucose_sd     n
    (int)    (dbl)      (dbl) (int)
  1     1  90.0000        NaN     1
  2     2 133.3333        NaN     1
  3     3 183.3333        NaN     1
  </code></pre>
  
  <p>Renaming "glucose" to "glucose_mean" works</p>
  
  <pre><code>df %&gt;% group_by(time) %&gt;%
    summarise(glucose_mean=mean(glucose, na.rm=TRUE),
          glucose_sd=sd(glucose, na.rm=TRUE),
          n=sum(!is.na(glucose)))
  
     time glucose_mean glucose_sd     n
    (int)        (dbl)      (dbl) (int)
  1     1      90.0000   10.00000     3
  2     2     133.3333   28.86751     3
  3     3     183.3333   28.86751     3
  </code></pre>