R | Input and Output

Many of the functions for input and output when using R are online.

  • load() load the datasets written with save
  • data(x) loads specified data sets
  • library(x) load add-on packages
  • read.table(file) reads a file in table format and creates a data frame from it; the default separator sep=”” is any whitespace; use header=TRUE to read the first line as a header of column names; use as.is=TRUE to prevent character vectors from being converted to factors; use comment.char=”” to prevent “#” from being interpreted as a comment; use skip=n to skip n lines before reading data; see the help for options on row naming, NA treatment, and others
  • read.csv(“filename”,header=TRUE) id. but with defaults set for
  • reading comma-delimited files
  • read.delim(“filename”,header=TRUE) id. but with defaults set
  • for reading tab-delimited files
  • read.fwf(file,widths,header=FALSE,sep=””,as.is=FALSE) read a table of fixed width formatted data into a ’data.frame’; widths is an integer vector, giving the widths of the fixed-width fields
  • save(file,…) saves the specified objects (…) in the XDR platform independent binary format
  • save.image(file) saves all objects
  • cat(…, file=””, sep=” “) prints the arguments after coercing to character; sep is the character separator between arguments
  • print(a, …) prints its arguments; generic, meaning it can have different methods for different objects
  • format(x,…) format an R object for pretty printing
  • write.table(x,file=””,row.names=TRUE,col.names=TRUE,sep=” “) prints x after converting to a data frame; if quote is TRUE, character or factor columns are surrounded by quotes (“); sep is the field separator; eol is the end-of-line separator; na is the string for missing values; use col.names=NA to add a blank column header to get the column headers aligned correctly for spreadsheet input
  • sink(file) output to file, until sink(). Most of the I/O functions have a file argument. This can often be a character string naming a file or a connection. file=”” means the standard input or output. Connections can include files, pipes, zipped files, and R variables. On windows, the file connection can also be used with description = “clipboard”.
  • To read a table copied from Excel, use x <- read.delim(“clipboard”) To write a table to the clipboard for Excel, use write.table(x,”clipboard”,sep=”\t”,col.names=NA)
  • For database interaction, see packages RODBC, DBI, RMySQL, RPgSQL, and ROracle.
  • See packages XML, hdf5, netCDF for reading other file formats.


Please note: I reserve the right to delete comments that are offensive or off-topic.

Leave a Reply

Your email address will not be published. Required fields are marked *

7 thoughts on “R | Input and Output

  1. Can you do more articles on what it takes to transition from an everyday analyst to someone in data science? I am enjoying the balance of math, programming, and domain knowledge you present on your site.


  2. Thanks for your marvelous posting! I seriously enjoyed reading it,
    you are a great writer. I have bookmarked your site. Please keep up the good work.