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.