R - capturing elements of R output into text files
I am trying to run an analysis by invoking R through the command line as follows:
R --no-save < SampleProgram.R > SampleProgram.opt
For example, consider the simple R program below:
mydata = read.csv("test.txt", header=T)
attach(mydata)
summary(Variable1)
q()
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()
This simple R program is going to be executed by a script that needs to use the summary statistics displayed for Variable1.
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).
The example above talks about the summary function. But, I need to do that with other commands as well (such as glm)开发者_Go百科
I am fairly new to R and was wondering if there was a way in R that I could do this?
Thanks for the help.
A simple way is to convert the output that you want to print to file, and convert it to a text string via capture.output. then you can simply cat the output to the file.
dat<-data.frame(a=rnorm(100),b=rnorm(100),c=rnorm(100))
mod<-lm(a~b+c,data=dat)
out<-capture.output(summary(mod))
cat(out,file="out.txt",sep="\n",append=TRUE)
out<-capture.output(vcov(mod))
cat(out,file="out.txt",sep="\n",append=TRUE)
this creates a file out.txt containing
Call:
lm(formula = a ~ b + c, data = dat)
Residuals:
Min 1Q Median 3Q Max
-2.67116 -0.81736 -0.07006 0.76551 2.91055
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.01196 0.11724 0.102 0.919
b 0.11931 0.12601 0.947 0.346
c -0.09085 0.13267 -0.685 0.495
Residual standard error: 1.171 on 97 degrees of freedom
Multiple R-squared: 0.0183, Adjusted R-squared: -0.001944
F-statistic: 0.9039 on 2 and 97 DF, p-value: 0.4084
(Intercept) b c
(Intercept) 0.0137444761 -0.0006929722 -0.0005721338
b -0.0006929722 0.0158784141 0.0042188705
c -0.0005721338 0.0042188705 0.0176018744
There are many ways:
- use
sink()
- open a file via
file()
and write results to it - place your code in a file and run it via
R CMD BATCH file.R
which creates output - explicitly write results data via
write.table()
or its variants likewrite.csv()
This is fairly elementary so you will probably benefit from reading the 'Introduction to R' manual, or one of the numerous books on R.
The simplest solution may be
R> X <- rnorm(100)
R> summary(X)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-2.480 -0.618 -0.223 -0.064 0.609 2.440
R> write.table(matrix(summary(X)[c(1,3,6)], nrow=1), \
file="/tmp/foo.txt", col.names=FALSE, row.names=FALSE)
R> system("cat /tmp/foo.txt")
-2.48 -0.223 2.44
R>
where I force the subset of summary()
to be a matrix of one row.
The important thing here is to learn that the summary function, as in:
summary(Variable1)
doesn't print the summary. It works out the summary and then returns it. The command line processor does the printing, just before popping up the next '>' prompt.
Lots of R functions work like that. Hence you can nearly always get return values by assignment. So if you do:
x = summary(Variable1)
then it won't get printed. But then type 'x' and it will. The command line prints the last thing evaluated.
Once you've got 'x', you can use the Import/Export methods to save for later.
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
You might also have a look at the R Data Import/Export manual (Section 1.2 Export to text files).
you do not need to export to a file, just use summary(x)[1] for min, summary(x)[2] for first quarter value.
精彩评论