1

I need to extract specific information from a list in R. My data is 'Benford' object and doesn't seem to be coercible using as_tibble()

My data looks as follows:

library(dplyr)
library(purrr)
library(benford.analysis)

# read data
data = read.csv("https://www2.census.gov/programs-surveys/popest/datasets/2010-2019/counties/totals/co-est2019-alldata.csv", header = T)

# ONLY SELECT THE NUMERIC COLUMN
x <- data %>% 
  select(CENSUS2010POP)

# SPLIT THE DATA INTO TWO GROUPS
x$CITY <- "CITY_A"
x[1500:3193,2] <- "CITY_B"

# SPLIT THE DATA ON THE CITY VARIABLE
dat_list <- split(x,x$CITY)

# APPLY THE BENFORD ANALYSIS TO THE NUMERICAL VALUES PER GROUP
output <- map(dat_list, ~benford(number.of.digits = 1, discrete = TRUE, sign = "positive", data = .x$CENSUS2010POP))

I wish to create a data frame with the CITY name i.e. CITY_A in one column and the $ MAD.conformity in another for each element in the list.

Any help on how I can pull these elements from my list and store them in a data frame?

TheGoat
  • 2,587
  • 3
  • 25
  • 58

1 Answers1

1

You can use purrr::map_dfr() to extract MAD.conformity on each iteration, row-bind the results, and add an .id column containing the names of each element of dat_list. Since dat_list was created by splitting on CITY, the names already correspond to the levels of CITY. (I'm not familiar with the structure of Benford objects, so you may have to tweak the $MAD.conformity index.)

output <- map_dfr(
  dat_list,
  ~ benford(
      number.of.digits = 1, 
      discrete = TRUE, 
      sign = "positive", 
      data = .x$CENSUS2010POP
    )$MAD.conformity,
  .id = "CITY"
)
zephryl
  • 14,633
  • 3
  • 11
  • 30