English 中文(简体)
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)

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 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
问题回答

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="
",append=TRUE)
out<-capture.output(vcov(mod))
cat(out,file="out.txt",sep="
",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 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.

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 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.





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

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 =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签