首先生成个例子
library(broom)
library(tidyverse)
models <- txhousing %>% group_by(city) %>%do(mod=lm(log2(sales)~factor(month),data = .,na.action = na.exclude))
models
model_sum1 <- map(models $mod,~glance(.))
model_sum1
class(model_sum1)
[1] “list”
model_sum1
[[1]]
A tibble: 1 x 12
r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC deviance1 0.530 0.500 0.282 17.9 1.50e-23 11 -22.2 70.5 112. 13.9
… with 2 more variables: df.residual , nobs
后面的省略了
就是生成了一个列表嵌套列表的数据
用下面的代码就可以直接转换成数据框,简单思路就是解嵌套,然后加上列名字。
model_sum1dematrix <- data.frame(matrix(unlist(model_sum1), nrow=46, byrow=T),stringsAsFactors=FALSE)
names <- colnames(model_sum1[[1]])
colnames(model_sum1dematrix) <- names
model_sum1dematrix
class(model_sum1dematrix)
[1] “data.frame”