I have a list of lists and I wish to extract specific elements from each list and store the results in a data frame.
For each VendorNum
I wish to extract n
and MAD.conformity
. In the example below there are 4 VendorNum
.
The list of lists can be obtained by running the following chunk:
result <- top_vendors %>%
split(.$VendorNum) %>%
map(~ benford(number.of.digits = 1, discrete = TRUE, sign = "positive", data = .x$Amount))
Using map_dfr
and extract
I can pull a single element from each list but keep running into errors when I add additional elements.
Here's a reprex of what I am working with:
library(dplyr)
library(purrr)
library(benford.analysis)
data(corporate.payment)
# FIND VENDORS WITH A SIGNIFICANT NUMBER OF ROWS
vendor_records <- corporate.payment %>%
group_by(VendorNum) %>%
summarise(records = n()) %>%
arrange(desc(records))
# FILTER FOR THESE VENDORS
top_vendors <- corporate.payment %>%
filter(VendorNum %in% c('3630','6661','2001','4984'))
result <- top_vendors %>%
split(.$VendorNum) %>%
map(~ benford(number.of.digits = 1, discrete = TRUE, sign = "positive", data = .x$Amount)) %>%
map_dfr(magrittr::extract, c("MAD.conformity"), .id = "VendorNum")
When I add a vector of elements to the extract function I get the following error:
map_dfr(magrittr::extract, c("MAD.conformity","n"), .id = "VendorNum")
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.
Run `rlang::last_error()` to see where the error occurred.
Warning message:
Outer names are only allowed for unnamed scalar atomic inputs
I'd be very grateful if you could help me figure out how to extract the extra elements into a data frame.
CodePudding user response:
to_extract <- list(MAD.conformity = c('MAD.conformity'),
n = c('info', 'n'))
top_vendors %>%
split(.$VendorNum) %>%
map(~ benford(number.of.digits = 1, discrete = TRUE, sign = "positive", data = .x$Amount)) %>%
map_df(~map(to_extract, ~.y[[.x]], .x), .id = 'VendorNum')
# A tibble: 4 x 3
VendorNum MAD.conformity n
<chr> <chr> <int>
1 2001 Nonconformity 4735
2 3630 Acceptable conformity 13361
3 4984 Acceptable conformity 4311
4 6661 Nonconformity 4133