英文:
created dataframe using R function, but want the results stored as separate variables instead of just printed
问题
如何修改函数以创建三个单独的数据框而不是打印它们?
要修改函数以创建三个单独的数据框而不是打印它们,你可以将每个模型的总结结果存储在一个列表中,然后返回这个列表。以下是修改后的函数代码:
table.sum <- function(x) {
table.x <- summary(x)
table.x <- as.data.frame(table.x$coefficients)
table.x <- table.x %>% mutate(Estimate = signif(Estimate, 2), `Std. Error` = signif(`Std. Error`, 2), `t value` = NULL, `Pr(>|t|)` = signif(`Pr(>|t|)`, digits = 2))
return(table.x) # 返回数据框而不是打印
}
mods <- list(full, bio, clim)
result_list <- lapply(mods, FUN = table.sum)
# result_list 现在包含了三个数据框,每个数据框对应一个模型的总结结果
现在,result_list
包含了三个数据框,每个数据框对应一个模型的总结结果,而不是将它们打印出来。
英文:
I am building models in R, and want to create a summary table of outputs for each model, specifically the estimates, standard errors and p-values.
Here is my current code with an example data frame and models.
speed<-c(0.5,0.1,0.3,0.4,0.9,0.2)
sex<-c(rep(c("F","M"),times=c(3,3)))
mass<-c(500,400,600,800,700,500)
year<-c(2000,2000,2001,2001,2002,2002)
temp.c<-c(0,2,3,1,3,0)
data<-data.frame(speed,sex,mass,year,temp.c)
data
full<-glm(formula = speed ~ sex + mass + year + temp.c, data=data,family=Gamma)
bio<-glm(formula = speed ~ sex + mass, data=data,family=Gamma)
clim<-glm(formula = speed ~ year + temp.c, data=data,family=Gamma)
table.sum<-function(x) {
table.x<-summary(x)
table.x<-as.data.frame(table.x$coefficients)
table.x<-table.x %>% mutate(Estimate=signif(Estimate,2),`Std. Error`= signif(`Std. Error`,2),`t value` = NULL,,`Pr(>|t|)`=signif(`Pr(>|t|)`,digits=2))
}
mods<-list(full,bio,clim)
lapply(mods,FUN=table.sum)
The current code just prints the data frames, like so:
[[1]]
Estimate Std. Error Pr(>|t|)
(Intercept) 2.6e+03 1.1e+04 0.85
sexM 1.6e+00 9.1e+00 0.89
mass -9.2e-03 2.2e-02 0.75
year -1.3e+00 5.3e+00 0.85
temp.c 9.4e-02 2.1e+00 0.97
[[2]]
Estimate Std. Error Pr(>|t|)
(Intercept) 2.6e+03 1.1e+04 0.85
sexM 1.6e+00 9.1e+00 0.89
mass -9.2e-03 2.2e-02 0.75
year -1.3e+00 5.3e+00 0.85
temp.c 9.4e-02 2.1e+00 0.97
[[3]]
Estimate Std. Error Pr(>|t|)
(Intercept) 2.6e+03 1.1e+04 0.85
sexM 1.6e+00 9.1e+00 0.89
mass -9.2e-03 2.2e-02 0.75
year -1.3e+00 5.3e+00 0.85
temp.c 9.4e-02 2.1e+00 0.97
How do I modify my function so it creates three separate data frames instead of printing them?
答案1
得分: 2
你可以像这样使用 list2env
:
ll <- lapply(mods, FUN=table.sum)
names(ll) <- c("模型1", "模型2", "模型3") # 将名称更改为你想要的任何名称
list2env(ll, envir = globalenv())
英文:
You use do list2env
like this:
ll <- lapply(mods, FUN=table.sum)
names(ll) <- c("Model 1", "Model 2", "Model 3") # name to whatever you want
list2env(ll, envir = globalenv())
答案2
得分: 2
We may use
library(collapse)
paste0(c("full", "bio", "mass"), "_table") %==% lapply(mods, FUN = table.sum)
Or with list2env
my_var <- lapply(mods, FUN = table.sum)
list2env(setNames(my_var, paste0(c("full", "bio", "mass"), "_table")), .GlobalEnv)
英文:
We may use
library(collapse)
paste0(c("full", "bio", "mass"), "_table") %==% lapply(mods,FUN=table.sum)
Or with list2env
my_var <- lapply(mods,FUN=table.sum)
list2env(setNames(my_var, paste0(c("full", "bio", "mass"), "_table")), .GlobalEnv)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论