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?