I am calculating summary statistics based on nested dataframes, and I am adding the statistics in unnested columns. Here is a toy example:
library(purrr)
library(dplyr)
library(tidyr)
df <- tribble(
~data,
tibble(pop = c(1, 2, 3)),
tibble(pop = c(4, 5, 6))
)
df2 <- df %>% mutate(median_pop = map(.x = data, ~ .x %>%
summarise(median(pop)))) %>%
unnest(median_pop) %>%
rename(median_pop = `median(pop)`)
This yields the desired result, but it requires the rename
function call in the last line to regenerate the median_pop column name created in the mutate
function call. It's clear to me that unnest
is somehow eliminating the median_pop column name, but it's not clear to me why that's the case or how to prevent it. It seems possible that either the names_repair or the names_sep arguments to unnest
might address the problem, but I do not understand how. Is it possible to retain column names when unnesting?