本文介绍了`unnest_wider` 多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 tibble,其中包含多个 list 列,我想 unnest_wider.

I have a tibble with multiple columns with multiple list columns I'd like to unnest_wider.

df1 <- tibble(
  gr = c('a', 'b', 'c'),
  values1 = list(1:2, 3:4, 5:6),
  values2 = list(1:2, 3:4, 5:6)
)

我尝试了很多方法都行不通,包括在 col

I have tried many approaches that have not worked including adding a vector into col

df1 %>% # unnest_wider doesn't take multiple inputs
  unnest_wider(col = c(values, values2),
               names_sep = c("_1", "_2"), 
               names_repair = "unique")

并尝试 mutate_at

df1 %>% # mutate_at doesn't send data
  mutate_at(vars(values, values2), 
            ~unnest_wider(col = .,
               names_sep = c("_1", "_2"), 
               names_repair = "unique"))

如何将多个列取消嵌套更宽?

How can I unnest multiple columns wider?

推荐答案

我用过的答案

df1 %>% 
  unnest() %>% 
  mutate(q_name = rep(c("1", "2"), nrow(.)/2)) %>% 
  pivot_wider(id_cols = gr, 
              names_from = q_name, 
              values_from = values1:values2)

这篇关于`unnest_wider` 多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 07:46