group_modify(), group_map(), and group_walk() throws errors that don't make sense

Sample code:

shuffle_rows = function(tbl) {
  tbl[sample(nrow(tbl)),] %>% return
}
sample_tbl = tibble(label=sample(c("a","b"),size=100,replace=T),
                    x=rnorm(100),
                    y=rnorm(100))
sample_tbl %>% group_by(label) %>% group_modify(shuffle_rows)

Running the sample code throws an error as below.

Error in as_group_map_function(.f) : 
  The function must accept at least two arguments. You can use ... to absorb unused components

Question 1. Why must the function (in this case, shuffle_rows) accept at least two arguments? I don't see the point.

Anyhow, I also tried this.

shuffle_rows = function(tbl, dummy_arg) {
  tbl[sample(nrow(tbl)),] %>% mutate(dummy_var = dummy_arg) %>% return
}
sample_tbl = tibble(label=sample(c("a","b"),size=100,replace=T),
                    x=rnorm(100),
                    y=rnorm(100))
sample_tbl %>% group_by(label) %>% group_modify(shuffle_rows, dummy_arg = 1)

But this also throws an error as below.

Error in (function (.x, .y)  : unused argument (dummy_arg = 1)

Question 2. Why am I getting this error and how can I solve it?

Hi @junghoonshin,
You can do either of these things to get your function to work:

# Either
sample_tbl %>% group_by(label) %>% group_modify(~shuffle_rows(.x))

# Or
shuffle_rows2 <- function(.data) {
  .data[sample(nrow(.data)),] %>% return
}

sample_tbl %>% group_by(label) %>% group_modify(~shuffle_rows2(.))

See this blog
https://drdoane.com/writing-pipe-friendly-functions/

HTH

3 Likes

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