R - 将R输出的元素捕获到文本文件中 [英] R - capturing elements of R output into text files

查看:168
本文介绍了R - 将R输出的元素捕获到文本文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过命令行调用R来运行分析,如下所示:

I am trying to run an analysis by invoking R through the command line as follows:

R --no-save < SampleProgram.R > SampleProgram.opt

例如,考虑下面的简单R程序:

For example, consider the simple R program below:

mydata = read.csv("test.txt", header=T)
attach(mydata)
summary(Variable1)
q()

输出显示在SampleProgram.opt中(仅部分显示):

The output is displayed in SampleProgram.opt (only partially shown):

> mydata = read.csv("test.txt", header=T)
> attach(mydata)
> summary(Variable1)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
   1.00    1.00    2.00    2.47    3.00    4.00
> q()

这个简单的R程序将由需要使用摘要的脚本为Variable1显示的统计信息。

This simple R program is going to be executed by a script that needs to use the summary statistics displayed for Variable1.

问题是:R中有没有办法捕获summary(Variable1)的输出并将结果写入输出文件?换句话说,我需要R来运行Variable1的摘要统计,捕获Min,Median和Max值,并将它们单独写入输出文本文件。在此示例中,输出文件应该只包含一个值为1.00,2.00,4.00的行(即Min,Median和Max值)。

The question is this: Is there any way in R to capture the output of summary(Variable1) and write the results into an output file? In other words, I need R to run the summary statistics for Variable1, capture the "Min", "Median" and "Max" values and write those alone to an output text file. In this example, the output file should contain only one line with the values "1.00, 2.00, 4.00" (i.e. the "Min", "Median" and "Max" values).

上面的例子介绍了汇总功能。但是,我需要这样做与其他命令(如glm)

The example above talks about the summary function. But, I need to do that with other commands as well (such as glm)

我是R的新手,想知道是否有一个方法在R,我可以这样做吗?

I am fairly new to R and was wondering if there was a way in R that I could do this?

感谢您的帮助。

推荐答案

您还可以访问 summary 命令的各个属性。例如

You can also access individual attributes of the summary command. For example

> x=summary(seq(1:10))
> attributes(x)
> attributes(x)
$names
[1] "Min."    "1st Qu." "Median"  "Mean"    "3rd Qu." "Max."   

$class
[1] "table"

> x["1st Qu."]
1st Qu. 
3.25

这篇关于R - 将R输出的元素捕获到文本文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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