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)
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.
You can also save the entire R console screen within the GUI by clicking on "Save to File..." under the menu "File." This saves the commands and the output to a text file, exactly as you see them on the screen.
Use R base function wirte. table() to export the data from R DataFrame to a text file. Besides this, R also provides a third-party package to write the text file.
In R, we can write data frames easily to a file, using the write. table() command. The first argument refers to the data frame to be written to the output file, the second is the name of the output file. By default R will surround each entry in the output file by quotes, so we use quote=F.
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:
sink()
file()
and write results to itR CMD BATCH file.R
which creates outputwrite.table()
or its variants like write.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With