Stuck in understanding ".by" behaviour. Help!

Hello community,

I am Mrinal, a Public Health Researcher, from India.

I am currently revising through r4ds (2e) and came by ".by". But the behaviors seems inconsistent. Or may be I am ignorant enough to not able to detect my human anomaly. Please help.

flights |> 
  select(carrier, dest, dep_delay, arr_delay) |> 
  group_by(dest, carrier) |> 
  summarise(
    add = mean(dep_delay, na.rm = T)
  ) |> 
  arrange((dest)) |> 
  slice_max(add, n =1)

flights |> 
  select(carrier, dest, dep_delay, arr_delay) |> 
  summarise(
    add = mean(dep_delay, na.rm = T),
    .by = c(dest, carrier)
    )|> 
  arrange((dest)) |> 
  slice_max(add, n =1)

Try comparing results right after summarise().

When used with .by, summarise() returns ungrouped frame and when you pass that to slice_max(..., n = 1), it keeps just a single row.

In your 1st example slice_max() recives grouped frame and applies slicing on every single group and number of returned rows matches the number of input groups. Not sure if you noticed or are aware that grouping in your 1st example also changed -- summarise() dropped last grouping level and silce_max() is called with a frame that's only grouped by dest.

2 Likes

Indeed! Results, though had same dimension, are not grouped when generated by .by.

summarise() has grouped output by 'dest'. You can override using the .groups argument.
Rows: 314
Columns: 3
Groups: dest [105]
dest <chr> "ABQ", "ACK", "ALB", "ANC", "ATL", "ATL", "ATL", "ATL", "ATL", "ATL", "ATL", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AVL", "AVL… carrier "B6", "B6", "EV", "UA", "9E", "DL", "EV", "FL", "MQ", "UA", "WN", "9E", "AA", "B6", "DL", "UA", "WN", "9E", "EV", "EV", "UA", "9E"…
$ add 13.7401575, 6.4566038, 23.6205251, 12.8750000, 0.9649123, 10.4063365, 22.4026442, 18.4456094, 9.3532028, 15.7864078, 2.3448276, 19…

Rows: 314
Columns: 3
dest <chr> "ABQ", "ACK", "ALB", "ANC", "ATL", "ATL", "ATL", "ATL", "ATL", "ATL", "ATL", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AVL", "AVL… carrier "B6", "B6", "EV", "UA", "DL", "MQ", "FL", "EV", "9E", "WN", "UA", "B6", "UA", "DL", "AA", "WN", "9E", "EV", "9E", "EV", "UA", "EV"…
$ add 13.7401575, 6.4566038, 23.6205251, 12.8750000, 10.4063365, 9.3532028, 18.4456094, 22.4026442, 0.9649123, 2.3448276, 15.7864078, 14…

Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.