【问题标题】:tidyr::nest - what is it good for?tidyr::nest - 它有什么用?
【发布时间】:2020-06-07 23:40:17
【问题描述】:

我经常使用tidyr::unnest。但我不使用nest;我无法弄清楚它解决了什么问题。 nest documentation 举个例子

as_tibble(iris) %>% nest(-Species)

但我不知道如何处理结果,除了立即应用unnest 并返回iris。我想到的其他任何事情——比如inner_joining 它——如果我改为group_byed 它,我也可以做到。我查看了其他使用 nest 的 SO 帖子,例如Irregular nest tidyverse,但他们没有教化。

nest - 它解决了什么问题?你能给我举一个使用nest 最直接解决的问题的例子吗?

PS

示例代码 as_tibble(iris) %>% nest(-Species) 现在 (tidyr 1.0.2) 给出警告。在不列出每个包含的列的情况下调用它的新的、正确的方法是什么? as_tibble(iris) %>% nest(-Species, cols = everything()) 没用。

【问题讨论】:

标签: r tidyr


【解决方案1】:

好问题!

Nest 是为了解决我们想要应用一个以复杂结构作为输入的函数的问题,我能想到的一个很好的例子是 lm 函数,正如优秀书籍 r4ds https://r4ds.had.co.nz/many-models.html#gapminder 中所展示的那样/p>

tidyverse 上还有一个新功能叫做nest_by,我展示了如何替换旧的nest 代码,但两者在正确的上下文中都非常有用

library(tidyverse)
library(gapminder)

by_country <- gapminder %>% 
  group_by(country, continent) %>% 
  nest()

by_country
#> # A tibble: 142 x 3
#> # Groups:   country, continent [142]
#>    country     continent data             
#>    <fct>       <fct>     <list>           
#>  1 Afghanistan Asia      <tibble [12 x 4]>
#>  2 Albania     Europe    <tibble [12 x 4]>
#>  3 Algeria     Africa    <tibble [12 x 4]>
#>  4 Angola      Africa    <tibble [12 x 4]>
#>  5 Argentina   Americas  <tibble [12 x 4]>
#>  6 Australia   Oceania   <tibble [12 x 4]>
#>  7 Austria     Europe    <tibble [12 x 4]>
#>  8 Bahrain     Asia      <tibble [12 x 4]>
#>  9 Bangladesh  Asia      <tibble [12 x 4]>
#> 10 Belgium     Europe    <tibble [12 x 4]>
#> # ... with 132 more rows


country_model <- function(df) {
  lm(lifeExp ~ year, data = df)
}


by_country <- by_country %>% 
  mutate(model = map(data, country_model))
by_country
#> # A tibble: 142 x 4
#> # Groups:   country, continent [142]
#>    country     continent data              model 
#>    <fct>       <fct>     <list>            <list>
#>  1 Afghanistan Asia      <tibble [12 x 4]> <lm>  
#>  2 Albania     Europe    <tibble [12 x 4]> <lm>  
#>  3 Algeria     Africa    <tibble [12 x 4]> <lm>  
#>  4 Angola      Africa    <tibble [12 x 4]> <lm>  
#>  5 Argentina   Americas  <tibble [12 x 4]> <lm>  
#>  6 Australia   Oceania   <tibble [12 x 4]> <lm>  
#>  7 Austria     Europe    <tibble [12 x 4]> <lm>  
#>  8 Bahrain     Asia      <tibble [12 x 4]> <lm>  
#>  9 Bangladesh  Asia      <tibble [12 x 4]> <lm>  
#> 10 Belgium     Europe    <tibble [12 x 4]> <lm>  
#> # ... with 132 more rows


# The new way is using nest_by

by_country_new <- gapminder %>% 
  nest_by(country,continent) %>% 
  mutate(model = list(country_model(data)))

by_country_new
#> # A tibble: 142 x 4
#> # Rowwise:  country, continent
#>    country     continent               data model 
#>    <fct>       <fct>     <list<tbl_df[,4]>> <list>
#>  1 Afghanistan Asia                [12 x 4] <lm>  
#>  2 Albania     Europe              [12 x 4] <lm>  
#>  3 Algeria     Africa              [12 x 4] <lm>  
#>  4 Angola      Africa              [12 x 4] <lm>  
#>  5 Argentina   Americas            [12 x 4] <lm>  
#>  6 Australia   Oceania             [12 x 4] <lm>  
#>  7 Austria     Europe              [12 x 4] <lm>  
#>  8 Bahrain     Asia                [12 x 4] <lm>  
#>  9 Bangladesh  Asia                [12 x 4] <lm>  
#> 10 Belgium     Europe              [12 x 4] <lm>  
#> # ... with 132 more rows

reprex package (v0.3.0) 于 2020 年 6 月 7 日创建

这也是在 iris 数据集上嵌套 Species 的新方法

library(tidyverse)


iris %>%
  group_by(Species) %>% 
  nest()
#> # A tibble: 3 x 2
#> # Groups:   Species [3]
#>   Species    data             
#>   <fct>      <list>           
#> 1 setosa     <tibble [50 x 4]>
#> 2 versicolor <tibble [50 x 4]>
#> 3 virginica  <tibble [50 x 4]>

reprex package (v0.3.0) 于 2020 年 6 月 7 日创建

【讨论】:

  • 谢谢@bruno。你解释得很好,提出了一个令人信服的理由。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 2011-01-14
  • 2023-03-11
  • 1970-01-01
  • 2011-05-27
  • 2010-10-17
  • 1970-01-01
相关资源
最近更新 更多