Remember that when writing to external files, you need to pay attention to the class of your object and what kind of output file you want to create.
Data frame to .csv:
A .csv file is a text file that can be read by a word processor or spreadsheet software. Use write.csv().
mydat <- data.frame(x=1:10, y=rnorm(x))
write.csv(mydat, file="mydat.csv")
Data frame (and other objects) to .Rdata:
An .Rdata file is a binary file specific to R. This is good for saving intermediate steps in data analysis that can be read in directly as R objects.
mydat <- data.frame(x=rnorm(10), y=rnorm(10))
a <- letters[1:10]
write(mydat, a, file="myobjects.Rdata")
Lines of information to text file
If you want to save lines of output to a text file, use cat(). Cat takes character strings, which you can assemble using paste(). It will write output line by line for each call, so if you want to add a line on to existing lines make sure to set append=TRUE.
x <- 244
line <- paste("The value of x is:", x, "\n", sep=" ")
cat(line, file="myoutput.txt")
cat(Sys.time(), file="my output.txt", append=TRUE)
cat("That is all. The end", file="myoutput.txt", append=TRUE)
Dumping output to a text file
Sometimes you just want all of the output, say from a linear model summary, to be saved in a text file.
xy <- data.frame(x=rnorm(10), y=rnorm(10))
lm.xy <- lm(y ~ x, data=xy)
sink("myoutput.txt") # opens connection to file
print(summary(lm.xy)) # prints regression output
sink() ## directs output back to console (screen)
sink(“filename”) open a connection to a file. Anything you print() after that will be sent to your file. Call sink() again to send output back to console.
Print plots to pdf
This is similar to the sink() example. Use pdf() to open a connection to a pdf file (or a “graphical device” of type pdf), then make your plots, then turn off the device.
xy <- data.frame(x=rnorm(10), y=rnorm(10))
pdf("xyplot.pdf") # opens pdf device
plot(x,y) # plots to file, nothing to screen
dev.off() # must turn off device or pdf wonʻt open
plot(x,y) # plots to screen
Put some thought into the storage of your data and your output and your script. Think about opening the project a year from now and being able to remember what you did, with enough information to be able to replicate it.
- Always save a copy of your data in text files (.csv). These are the most robust to archiving. No matter how file formats change, you can always open a text file.
- Also save a copy of all of your output as pdfs or txt files. This is what you will refer to when you are revising your manuscript, etc. Even if your script is working, itʻs always a good idea to save the output so you know what you got.
- Of course save a clean, commented, working copy of your script. Clean it up, remove extraneous objects and code, and run it from source after restarting R. Save all of these items in a clean folder with only the essential elements, inputs and outputs.
Reproducible results!