Overriding of group_by function and unable to use"groups" function

antibioticDT.pdf (75.8 KB)

last_administration_day <- antibioticDT %>%
group_by(patient_id, antibiotic_type, route, day_given) %>%
summarise(last_administration_day = max(day_given))

*It is deleting the "day_given" column.

  • Error Message -- summarise() has grouped output by 'patient_id', 'antibiotic_type', 'route'. You can override using the .groups argument.

*The "groups" argument isn't working too.

last_administration_day <- antibioticDT %>%
groups(patient_id, antibiotic_type, route, day_given) %>%
summarise(last_administration_day = max(day_given))

  • Error Message -- Error in groups(., patient_id, antibiotic_type, route, day_given) :
    unused arguments (patient_id, antibiotic_type, route, day_given)

It's a bit strange, in my experience, to have a column in the group by and the summarise

The groups() function does not group the data. It just returns a list of the grouping variables.

library(tidyverse)

mtcars |> group_by(cyl, am) |> groups()
#> [[1]]
#> cyl
#> 
#> [[2]]
#> am

Created on 2023-05-10 with reprex v2.0.2

If you group by patient_id, antibiotic_type, route and day administered, each group will consist of a single patient for a specific antibiotic by a specific route on a specific day. With just one day in each group, the maximum value would equal that day. If you want the last day that each patient received a specific antibiotic by a specific route, leave day_given out of group_by().

Right, it's suggesting OP use the argument .groups:

You can override using the .groups argument.

(And then, of course, what you've described in the last post should work).