[英]Unnest one of several list columns in dataframe
I have a tibble with several list columns and I'd like to only unnest one of them. 我有几个列表列,我只想取消其中一个列表。
Example 例
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]>
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. 现在我想保留嵌套data
列,但是sum_data
列,没有专门指定sum_data
每个列sum_data
,也没有sum_data
整个数据集,然后重新解析数据列。
Desired outcome 期望的结果
# 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
According to unnest
, the argument ...
根据unnest
,争论...
Specification of columns to nest. 要嵌套的列的规范。 Use bare variable names or functions of variables. 使用裸变量名称或变量函数。 If omitted, defaults to all list-cols. 如果省略,则默认为所有list-cols。
Therefore, we could specify the column name to be unnest
ed after the rename_all
因此,我们可以指定列名是unnest
编后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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.