将嵌套 tibble 的所有列取消嵌套到 tibbles 列表

Unnest all columns of nested tibble to a list of tibbles

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

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

有没有办法不将每一列单独取消嵌套作为一个新的标题?是否可以将所有列立即取消嵌套到新的 tibbles 列表中?一个可以用于其他功能(比如写一个 excel 文件)?

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")

编辑

再次阅读你的问题,我错过了最后一部分。不过现在应该差不多就是你要找的了。


像这样?

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