0

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?
lf_araujo
  • 1,991
  • 2
  • 16
  • 39
  • 1
    What is the desired output? Are you trying to recreate the existing `group_split` function? Or maybe `group_map`? – MrFlick Jul 06 '22 at 18:27
  • sounds like group_split is what I want, will look into it. I want small data sets based in the existing groups. – lf_araujo Jul 06 '22 at 18:34
  • Ok, group_split is what I want, could you convert your comment into an answer so I can accept it? Thanks – lf_araujo Jul 06 '22 at 18:45

0 Answers0