Why dplyr :: do () resolves. to link to the current group but dplyr :: summary () is not?

I am currently scratching my head about a specific behavior in dplyr

which I cannot figure out. For grouped data, do

allows operations .

for the current group, but c summarise

.

applies to all data, not every group. Why is this so?

library(dplyr)
df <- data.frame( 
  hour     = factor(rep(1:24, each = 21)),
  price    = runif(504, min = -10, max = 125)
)

df %>% group_by(hour) %>% 
  summarise(mean(price))
# # A tibble: 24 x 2
#     hour `mean(price)`
#   <fctr>         <dbl>
# 1      1      58.78788
# 2      2      55.51119
# 3      3      54.44470
# [...]


df %>% group_by(hour) %>% 
  summarise(mean(.$price))
# # A tibble: 24 x 2
#      hour `mean(.$price)`
#    <fctr>           <dbl>
#  1      1        54.66447
#  2      2        54.66447
#  3      3        54.66447
# [...]

mean(df$price)
# [1] 54.66447


df %>% group_by(hour) %>% 
  do(as.data.frame(mean(.$price)))
# # A tibble: 24 x 2
# # Groups:   hour [24]
#      hour `mean(.$price)`
#    <fctr>           <dbl>
#  1      1        58.78788
#  2      2        55.51119
#  3      3        54.44470
# [...]

      

As you can see, only summarise

without .

and do

with the .

expected results are produced (i.e., they are aggregated by the grouping variable). summarise

s .

just returns the total mean

.

I know why the questions are perhaps not the most appropriate questions for SO, but since dplyr

it is supposed to be intuitive, and it doesn't strike me as intuitive, I hope everything is in order.

+3


source to share





All Articles