CodexBloom - Programming Q&A Platform

implementing using `purrr::map()` to iterate over a list of data frames and produce a summary table in R

👀 Views: 0 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-27
r dplyr purrr R

I'm upgrading from an older version and I'm trying to use `purrr::map()` to iterate over a list of data frames and generate a summary table that includes the mean and standard deviation of a particular numeric column... However, I'm working with issues getting the results into a tidy format. Here's a simplified version of what I have: ```r library(dplyr) library(purrr) # Sample list of data frames list_of_dfs <- list( df1 = data.frame(value = c(1, 2, 3, NA)), df2 = data.frame(value = c(4, 5, NA, 7)), df3 = data.frame(value = c(NA, NA, 3, 6)) ) # Attempting to summarize each data frame summary_results <- map(list_of_dfs, ~ summarise(.x, mean_value = mean(value, na.rm = TRUE), sd_value = sd(value, na.rm = TRUE) )) ``` When I run this code, I get a list of data frames as output, but I need a single data frame that contains all the results. I also tried using `bind_rows()` at the end, but I encountered an behavior: "behavior: `data` must be a data frame, not a list". I don't understand how to combine the results correctly. Any suggestions on how to properly combine the summaries into one data frame while keeping the names of the original data frames as a new column? I appreciate any help! I'm working on a CLI tool that needs to handle this. Has anyone dealt with something similar? I've been using R for about a year now. Could this be a known issue?