取消嵌套数据框中的几个列表列之一 [英] Unnest one of several list columns in dataframe

查看:24
本文介绍了取消嵌套数据框中的几个列表列之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个列表列的小标题,我只想取消嵌套其中的一个.

I have a tibble with several list columns and I'd like to only unnest one of them.

示例

library(dplyr)
library(purrr)
library(tidyr)
library(stringr)

iris %>% 
  group_by(Species) %>% 
  nest() %>% 
  mutate(sum_data = map(data,
                        ~.x %>% 
                          summarize_all(mean) %>% 
                          rename_all(funs(str_c("Mean.", .))))) 

# A tibble: 3 x 3
#      Species              data         sum_data
#       <fctr>            <list>           <list>
# 1     setosa <tibble [50 x 4]> <tibble [1 x 4]>
# 2 versicolor <tibble [50 x 4]> <tibble [1 x 4]>
# 3  virginica <tibble [50 x 4]> <tibble [1 x 4]>

现在我想保留嵌套的 data 列,但取消嵌套 sum_data 列,而不在 sum_data 中特别指定每个列名,并且还无需取消嵌套整个数据集,然后重新嵌套数据列.

Now I would like to keep the nested data column, but unnest the sum_data column, without specifically specifying each columnname in sum_data and also without unnesting the whole dataset and then renesting the data column.

预期结果

# A tibble: 3 x 6
#      Species              data Mean.Sepal.Length Mean.Sepal.Width Mean.Petal.Length Mean.Petal.Width
#       <fctr>            <list>             <dbl>            <dbl>             <dbl>            <dbl>
# 1     setosa <tibble [50 x 4]>             5.006            3.428             1.462            0.246
# 2 versicolor <tibble [50 x 4]>             5.936            2.770             4.260            1.326
# 3  virginica <tibble [50 x 4]>             6.588            2.974             5.552            2.026

推荐答案

根据unnest,参数...

要嵌套的列的规范.使用裸变量名或变量的函数.如果省略,则默认为所有列表列.

Specification of columns to nest. Use bare variable names or functions of variables. If omitted, defaults to all list-cols.

因此,我们可以在 rename_all

iris %>
  ... #op's code
  ...

  rename_all(funs(str_c("Mean.", .))))) %>%
  unnest(sum_data)
# A tibble: 3 x 6
#  Species    data              Mean.Sepal.Length Mean.Sepal.Width Mean.Petal.Length Mean.Petal.Width
#  <fctr>     <list>                        <dbl>            <dbl>             <dbl>            <dbl>
#1 setosa     <tibble [50 x 4]>              5.01             3.43              1.46            0.246
#2 versicolor <tibble [50 x 4]>              5.94             2.77              4.26            1.33 
#3 virginica  <tibble [50 x 4]>              6.59             2.97              5.55            2.03 

这篇关于取消嵌套数据框中的几个列表列之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆