created dataframe using R function, but want the results stored as separate variables instead of just printed

huangapple go评论51阅读模式
英文:

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&lt;-c(0.5,0.1,0.3,0.4,0.9,0.2)
sex&lt;-c(rep(c(&quot;F&quot;,&quot;M&quot;),times=c(3,3)))
mass&lt;-c(500,400,600,800,700,500)
year&lt;-c(2000,2000,2001,2001,2002,2002)
temp.c&lt;-c(0,2,3,1,3,0)
data&lt;-data.frame(speed,sex,mass,year,temp.c)
data

full&lt;-glm(formula = speed ~ sex + mass + year + temp.c, data=data,family=Gamma)
bio&lt;-glm(formula = speed ~ sex + mass, data=data,family=Gamma)
clim&lt;-glm(formula = speed ~ year + temp.c, data=data,family=Gamma)

table.sum&lt;-function(x) {
  
  table.x&lt;-summary(x)
  table.x&lt;-as.data.frame(table.x$coefficients) 
  table.x&lt;-table.x %&gt;% mutate(Estimate=signif(Estimate,2),`Std. Error`= signif(`Std. Error`,2),`t value` = NULL,,`Pr(&gt;|t|)`=signif(`Pr(&gt;|t|)`,digits=2))
  }

mods&lt;-list(full,bio,clim)
lapply(mods,FUN=table.sum)

The current code just prints the data frames, like so:

[[1]]
            Estimate Std. Error Pr(&gt;|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(&gt;|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(&gt;|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 &lt;- lapply(mods, FUN=table.sum)
names(ll) &lt;- c(&quot;Model 1&quot;, &quot;Model 2&quot;, &quot;Model 3&quot;) # 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(&quot;full&quot;, &quot;bio&quot;, &quot;mass&quot;), &quot;_table&quot;) %==% lapply(mods,FUN=table.sum)

Or with list2env

my_var &lt;- lapply(mods,FUN=table.sum)
list2env(setNames(my_var, paste0(c(&quot;full&quot;, &quot;bio&quot;, &quot;mass&quot;), &quot;_table&quot;)), .GlobalEnv)

</details>



huangapple
  • 本文由 发表于 2023年2月24日 03:39:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定