CodexBloom - Programming Q&A Platform

Difficulty extracting key-value pairs from a list column in data frame using purrr in R

👀 Views: 326 đŸ’Ŧ Answers: 1 📅 Created: 2025-07-16
r purrr dplyr R

I've searched everywhere and can't find a clear answer. I'm working on a personal project and I am trying to extract key-value pairs from a list column in a data frame using the `purrr` package. I have a data frame with a column that contains lists of named vectors, and I want to transform this column into separate columns for each key in the list. However, I am running into issues with applying the `map_dfr` function correctly. My data frame looks like this: ```r library(dplyr) library(purrr) df <- data.frame(id = 1:3, details = I(list( c(name='Alice', age=30), c(name='Bob', age=25), c(name='Charlie', age=35) ))) ``` When I try to use `map_dfr` as follows: ```r result <- df %>% mutate(details = map(details, ~ as.data.frame(t(.)))) %>% unnest(details) ``` I receive the following warning: ``` Warning message: In `mutate()`: question while computing `details = map(details, ~as.data.frame(t(.)))`. The `...` argument must be empty. ``` I believe the scenario might be with how I am trying to transpose the list elements, but I'm not sure how to resolve it. Additionally, I want to ensure that the resulting data frame has the correct structure with separate columns for each key in the list. What could be the best approach to achieve this? Any help would be greatly appreciated! For context: I'm using R on Ubuntu. This is part of a larger CLI tool I'm building. Has anyone else encountered this?