I am trying to make a function that takes a grouped tbl (or any other data concept) and loop across all of each grouped tbl.
It will be called like so:
mydata |>
filter( a > 10 ) |>
group_by(zig) |>
myfunc()
Inside it, I have something like:
if (dplyr::is_grouped_df(.data)) {
model <- list()
levels = dplyr::group_keys(.data)[[1]]
col = dplyr::group_vars(.data) |> as.character()
for (i in levels) {
# print(eval(select(.data,substitute(col) == substitute(i) ))
# print(.data[substitute(col)== substitute(i),])
}
}
The two commented lines does not work, and I tried a few iterations. I cant make dplyr::select or the data.table syntax detect that they should be filtering by the contents of col and i, not by col and i itself.
If it is not clear, col holds the name of the column I want to filter the data and i is holding the level to which I want the data filtered by.
- What am I missing there?