I wish to run a Benford analysis per group for my data.
I wish to extract the MAD.conformity and n per group and store the output in a data frame where there would be one row per group with 3 columns, 1st column - VendorNum, 2nd column = n and 3rd column = MAD.conformity
I can extract the MAD.conformity metric but when I add extra metrics to my extract I get the following error:
Any help to get the 3 metrics into a data frame would be greatly appreciated
Error:
! Column names `MAD.conformity`, `MAD.conformity`, and `MAD.conformity` must not be duplicated.
Use .name_repair to specify repair.
Caused by error in `stop_vctrs()`:
! Names must be unique.
x These names are duplicated:
* "MAD.conformity" at locations 1, 2, 3, and 4.
The call to inner_join() will keep columns both from the LHS and RHS of the join, but only the rows where the the keys match, this in effect filters the larger data frame by only the top (4, in this case) vendor records while simultaneously bringing in the counts.
Rather than splitting, group_nest() will allow you operate by-group like split() but in a more organized manner. This will create a new column called data, on to which we can map the benford() function and then use a combination of map_chr() and pluck() to extract the result you wanted.
Look at the pattern for Many Models in R4DS. I get the same error if I use purr::map to add columns for both broom:tidy() and broom::glance() because they have duplicate output columns that collide in the grouped and nested output data frame.