I'm currently working on a project and I'm having a problem which I can't find the answer for. So this is what I'm trying to do:
df
| sex | age | values | nationality |
| -------- | -------- | ------ | ------------ |
| male | 18 | 3 | german |
| male | 40 | 4 | italian |
| female | 18 | 1 | belgian |
| male | 40 | 2 | american |
col <- "values"
groups <- c("sex", "age")
f <- function(df, col, groups) {
df <- df |>
group_by_at(groups) |>
filter(!!col == min(col) |>
ungroup()
}
# Solution I want:
f(df, col, groups)
| sex | age | values | nationality |
| -------- | -------- | ------ | ------------ |
| male | 18 | 3 | german |
| female | 18 | 1 | belgian |
| male | 40 | 2 | american |
The part that is giving me headaches is the use of col in the fucntion.
I already found the solution to use !!col instead of col for the first use. But I can't find the solution for min(col). I already tried many things like min(!!col), min(UQ(col) and a few others.
Thanks for your help.
Ps:
This is basically what my function should be doing, just with sex, age as a parameter (vector) and col as a parameter:
df <- df |>
group_by(sex, age) |>
filter(col == min(col)) |>
ungroup()