How to Get Your Data Out of R

For the same reason that it’s convenient to import data into R using CSV (comma-separated values) files, it’s also convenient to export results from R to other applications in CSV format. To create a CSV file, use the write.csv() function. In the same way that read.csv() is a special case of read.table(), write.csv() is a special case of write.table().

To interactively export data from R for pasting into other applications, you can use writeClipboard() or write.table(). The writeClipboard() function is useful for exporting vector data. For example, to export the names of the built-in dataset iris, try the following:

> writeClipboard(names(iris))

This function doesn’t produce any output to the R console, but you can now paste the vector into any application. For example, if you paste this into Excel, you’ll have a column of five entries that contains the names of the iris data.

image0.png

To write tabular data to the Clipboard, you need to use write.table() with the arguments file="clipboard", sep="\t", and row.names=FALSE:

> write.table(head(iris), file="clipboard", sep="\t", row.names=FALSE)

Again, this doesn’t produce output to the R console, but you can paste the data into a spreadsheet.

image1.png
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.