将嵌套的小标题的所有列取消嵌套到小标题列表 [英] Unnest all columns of nested tibble to a list of tibbles

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

问题描述

我正在为数据集中的每个组拟合模型。我通过分组变量嵌套数据,然后使用map将模型拟合到每个组。然后,将整理过的模型信息作为列存储在嵌套的小块中。

I am fitting a model to each group in a dataset. I am nesting the data by the grouping variable and then using map to fit a model to each group. Then I store the tidied model information as columns in a nested tibble.

我想将这些列中的每一个另存为自己的文件,此示例将它们保存为工作表在Excel工作簿中。

I'd like to save each of these columns as its own file, this example saves them as sheets in a excel workbook.

是否有一种方法可以不将每个列都嵌套为新小标题?是否可以立即将所有列都取消嵌套到新的小标题列表中?可以在其他功能(例如编写excel文件)中使用的那个吗?

Is there a way to not to unnest each column individually as a new tibble? Can all columns be unnested at once to a new list of tibbles? One that can be used in other functions (like writing an excel file)?

library(tidyverse)
library(broom)
data(mtcars)
df <- mtcars

nest.df <- df %>% nest(-carb) 

results <- nest.df %>% 
  mutate(fit = map(data, ~ lm(mpg ~ wt, data=.x)),
         tidied = map(fit, tidy),
         glanced = map(fit, glance),
         augmented = map(fit, augment))


glanced.df <- results %>% 
  unnest(glanced, .drop=T)

tidied.df <- results %>% 
  unnest(tidied, .drop=T)

augmented.df <- results %>% 
  unnest(augmented, .drop=T)

myList <- list(glanced.df, tidied.df, augmented.df)
names(myList) <- c("glance", "tidy", "augment")

openxlsx::write.xlsx(myList, file = "myResults.xlsx")


推荐答案

编辑



再次阅读您的问题,我错过了最后一个rt。

Edit

read your question again, I missed the last part. But now it should be pretty much what you are searching for.

像这样吗?

library(tidyverse)
library(broom)
data(mtcars)
df <- mtcars

nest.df <- df %>% nest(-carb) 

results <- nest.df %>% 
  mutate(fit = map(data, ~ lm(mpg ~ wt, data=.x)),
         tidied = map(fit, tidy),
         glanced = map(fit, glance),
         augmented = map(fit, augment))
results %>% select(glanced, tidied, augmented ) %>% 
map(~bind_rows(.))
#> $glanced
#> # A tibble: 6 x 11
#>   r.squared adj.r.squared  sigma statistic  p.value    df logLik    AIC
#>       <dbl>         <dbl>  <dbl>     <dbl>    <dbl> <int>  <dbl>  <dbl>
#> 1   0.696           0.658   2.29  18.3      2.70e-3     2 -21.4    48.7
#> 2   0.654           0.585   3.87   9.44     2.77e-2     2 -18.2    42.4
#> 3   0.802           0.777   2.59  32.3      4.62e-4     2 -22.6    51.1
#> 4   0.00295        -0.994   1.49   0.00296  9.65e-1     2  -3.80   13.6
#> 5   0               0     NaN     NA       NA           1 Inf    -Inf  
#> 6   0               0     NaN     NA       NA           1 Inf    -Inf  
#> # … with 3 more variables: BIC <dbl>, deviance <dbl>, df.residual <int>
#> 
#> $tidied
#> # A tibble: 10 x 5
#>    term        estimate std.error statistic      p.value
#>    <chr>          <dbl>     <dbl>     <dbl>        <dbl>
#>  1 (Intercept)   27.9       2.91     9.56     0.0000118 
#>  2 wt            -3.10      0.724   -4.28     0.00270   
#>  3 (Intercept)   44.8       6.49     6.90     0.000982  
#>  4 wt            -7.81      2.54    -3.07     0.0277    
#>  5 (Intercept)   39.4       3.09    12.7      0.00000137
#>  6 wt            -5.92      1.04    -5.68     0.000462  
#>  7 (Intercept)   17.5      22.1      0.791    0.574     
#>  8 wt            -0.312     5.73    -0.0544   0.965     
#>  9 (Intercept)   19.7     NaN      NaN      NaN         
#> 10 (Intercept)   15       NaN      NaN      NaN         
#> 
#> $augmented
#> # A tibble: 32 x 9
#>      mpg    wt .fitted .se.fit .resid  .hat .sigma .cooksd .std.resid
#>    <dbl> <dbl>   <dbl>   <dbl>  <dbl> <dbl>  <dbl>   <dbl>      <dbl>
#>  1  21    2.62    19.7   1.17   1.25  0.263   2.38 0.0730       0.639
#>  2  21    2.88    19.0   1.04   2.04  0.205   2.29 0.129        1.00 
#>  3  14.3  3.57    16.8   0.761 -2.50  0.111   2.23 0.0838      -1.16 
#>  4  19.2  3.44    17.2   0.796  1.99  0.121   2.31 0.0594       0.929
#>  5  17.8  3.44    17.2   0.796  0.594 0.121   2.43 0.00527      0.277
#>  6  10.4  5.25    11.6   1.22  -1.20  0.283   2.39 0.0760      -0.620
#>  7  10.4  5.42    11.1   1.32  -0.663 0.333   2.43 0.0315      -0.355
#>  8  14.7  5.34    11.3   1.27   3.39  0.310   1.90 0.714        1.78 
#>  9  13.3  3.84    16.0   0.725 -2.67  0.100   2.20 0.0842      -1.23 
#> 10  15.8  3.17    18.0   0.895 -2.24  0.153   2.27 0.102       -1.06 
#> # … with 22 more rows

这篇关于将嵌套的小标题的所有列取消嵌套到小标题列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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