在 R 中打印或捕获多个对象 [英] Print or capturing multiple objects in R

查看:42
本文介绍了在 R 中打印或捕获多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 脚本中有多个回归,并希望将回归摘要附加到单个文本文件输出中.我知道我可以使用以下代码为一个回归摘要执行此操作,但我将如何为多个执行此操作?

I have multiple regressions in an R script and want to append the regression summaries to a single text file output. I know I can use the following code to do this for one regression summary, but how would I do this for multiple?

rpt1 <- summary(fit)
capture.output(rpt1, file = "results.txt")

我不想在同一个脚本中多次使用它(对于 rpt1、rpt2 等),因此每个结果都有单独的文本文件.我确信这很容易,但我仍在学习 R 绳索.有什么想法吗?

I would prefer not to have to use this multiple times in the same script (for rpt1, rpt2, etc.), and thus have separate text files for each result. I'm sure this is easy, but I'm still learning the R ropes. Any ideas?

推荐答案

您可以将结果存储为列表,然后使用 capture.output

You can store the result as a list and then use the capture.output

fit1<-lm(mpg~cyl,data=mtcars)
fit2<-lm(mpg~cyl+disp,data=mtcars)
myresult<-list(fit1,fit2)
capture.output(myresult, file = "results.txt")

这篇关于在 R 中打印或捕获多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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