tags:

views:

49

answers:

3

I have several questions to do with handling some data in R:

  1. I am using this statement: detailsTable <- read.table(file=commandArgs()[6], header=TRUE, col.names=c("a", "b", "c", "d", "e")) and it seems that the table is not being loaded correctly... but if I specify the path of the file I am loading excplicitly then all goes well. What am I doing wrong?

  2. I plot the data contained in that table mentioned above. How do I save the plot (eg: plot.savePDF("plot.pdf")) to a PDF file?

  3. How could I redirect the output of, for example, cor(detailsTable$a, detailsTable$b) to a file? and how do I write a simple string to a file. eg: "Correlation of the data: " + cor(...)

  4. How do I plot the line of best fit on an existing plot?

All of this is in R.

Many thanks to anyone who can help,

ExtremeCoder

+1  A: 

How to save the plot you're producing depends on which plotting system you're using. Assuming it's base graphics, you need to start a pdf graphics device, then plot to it.

pdf(file = "path/file.pdf", width = 5, height = 5)
...
#plotting commands here
...
dev.off()
JoFrhwld
+3  A: 

I plot the data contained in that table mentioned above. How do I save the plot (eg: plot.savePDF("plot.pdf")) to a PDF file?

 pdf("filename.pdf")
 plot(...)
 dev.off()

How could I redirect the output of, for example, cor(detailsTable$a, detailsTable$b) to a file? and how do I write a simple string to a file. eg: "Correlation of the data: " + cor(...)

check the write.table manual page (?write.table)

How do I plot the line of best fit on an existing plot?

x <- 1:10
y <- 2 * x + runif(10) 
plot (x, y, pch=20)
fit <- glm(y~x)
coefs <- coef(fit)
abline(coefs, lwd=2, col='red')
# Or also, without finding the coefficients
abline(fit, lwd=2, col='red')
nico
You might also want to check out the `cat` command for writing stuff to files. Also, `abline(fit)` will also do the trick (you don't need to grok out the coeffs).
Jonathan Chang
@Jonathan Chang: Oh cool! I'll update the answer
nico
+2  A: 

You can redirect output using sink().

dan