R Reference Card

ADVERTISEMENT

R Reference Card
character or factor columns are surrounded by quotes ("); sep is the
Indexing lists
field separator; eol is the end-of-line separator; na is the string for
x[n]
list with elements n
th
missing values; use col.names=NA to add a blank column header to
element of the list
x[[n]]
n
by Tom Short, EPRI PEAC, 2004-11-07
get the column headers aligned correctly for spreadsheet input
x[["name"]] element of the list named "name"
Granted to the public domain. See for the source and latest
sink(file) output to file, until sink()
x$name
id.
version. Includes material from R for Beginners by Emmanuel Paradis (with
Most of the I/O functions have a file argument. This can often be a charac-
Indexing matrices
permission).
ter string naming a file or a connection. file="" means the standard input or
element at row i, column j
x[i,j]
output. Connections can include files, pipes, zipped files, and R variables.
x[i,]
row i
On windows, the file connection can also be used with description =
x[,j]
column j
x[,c(1,3)] columns 1 and 3
"clipboard". To read a table copied from Excel, use
Getting help
x["name",] row named "name"
x <- read.delim("clipboard")
Most R functions have online documentation.
To write a table to the clipboard for Excel, use
Indexing data frames (matrix indexing plus the following)
help(topic) documentation on topic
x[["name"]] column named "name"
write.table(x,"clipboard",sep=" t",col.names=NA)
?topic id.
id.
For database interaction, see packages RODBC, DBI, RMySQL, RPgSQL, and
x$name
help.search("topic") search the help system
ROracle. See packages XML, hdf5, netCDF for reading other file formats.
apropos("topic") the names of all objects in the search list matching
Data creation
the regular expression ”topic”
Variable conversion
help.start() start the HTML version of help
c(...) generic function to combine arguments with the default forming a
str(a) display the internal *str*ucture of an R object
vector; with recursive=TRUE descends through lists combining all
as.array(x), as.data.frame(x), as.numeric(x),
summary(a) gives a “summary” of a, usually a statistical summary but it is
elements into one vector
as.logical(x), as.complex(x), as.character(x),
generic meaning it has different operations for different classes of a
from:to generates a sequence; “:” has operator priority; 1:4 + 1 is “2,3,4,5”
... convert type; for a complete list, use methods(as)
ls() show objects in the search path; specify pat="pat" to search on a
seq(from,to) generates a sequence by= specifies increment; length=
Variable information
pattern
specifies desired length
ls.str() str() for each variable in the search path
seq(along=x) generates 1, 2, ..., length(along); useful for for
is.na(x), is.null(x), is.array(x), is.data.frame(x),
dir() show files in the current directory
loops
is.numeric(x), is.complex(x), is.character(x),
methods(a) shows S3 methods of a
... test for type; for a complete list, use methods(is)
rep(x,times) replicate x times; use each= to repeat “each” el-
methods(class=class(a)) lists all the methods to handle objects of
length(x) number of elements in x
ement of x each times; rep(c(1,2,3),2) is 1 2 3 1 2 3;
class a
rep(c(1,2,3),each=2) is 1 1 2 2 3 3
dim(x) Retrieve or set the dimension of an object; dim(x) <- c(3,2)
create a data frame of the named or unnamed
dimnames(x) Retrieve or set the dimension names of an object
Input and output
data.frame(...)
arguments; data.frame(v=1:4,ch=c("a","B","c","d"),n=10);
nrow(x) number of rows; NROW(x) is the same but treats a vector as a one-
load() load the datasets written with save
row matrix
shorter vectors are recycled to the length of the longest
data(x) loads specified data sets
ncol(x) and NCOL(x) id. for columns
list(...)
create a list of the named or unnamed arguments;
library(x) load add-on packages
class(x) get or set the class of x; class(x) <- "myclass"
list(a=c(1,2),b="hi",c=3i);
read.table(file) reads a file in table format and creates a data
array
with
data
specify
dimensions
like
unclass(x) remove the class attribute of x
array(x,dim=)
x;
frame from it; the default separator sep="" is any whitespace; use
dim=c(3,4,2); elements of x recycle if x is not long enough
attr(x,which) get or set the attribute which of x
header=TRUE to read the first line as a header of column names; use
attributes(obj) get or set the list of attributes of obj
matrix(x,nrow=,ncol=) matrix; elements of x recycle
as.is=TRUE to prevent character vectors from being converted to fac-
factor(x,levels=) encodes a vector x as a factor
Data selection and manipulation
tors; use comment.char="" to prevent "#" from being interpreted as
gl(n,k,length=n * k,labels=1:n) generate levels (factors) by spec-
a comment; use skip=n to skip n lines before reading data; see the
which.max(x) returns the index of the greatest element of x
ifying the pattern of their levels; k is the number of levels, and n is
help for options on row naming, NA treatment, and others
which.min(x) returns the index of the smallest element of x
the number of replications
read.csv("filename",header=TRUE) id. but with defaults set for
rev(x) reverses the elements of x
expand.grid() a data frame from all combinations of the supplied vec-
reading comma-delimited files
sort(x) sorts the elements of x in increasing order; to sort in decreasing
tors or factors
read.delim("filename",header=TRUE) id. but with defaults set
order: rev(sort(x))
rbind(...) combine arguments by rows for matrices, data frames, and
for reading tab-delimited files
cut(x,breaks) divides x into intervals (factors); breaks is the number
others
read.fwf(file,widths,header=FALSE,sep="",as.is=FALSE)
of cut intervals or a vector of cut points
cbind(...) id. by columns
read a table of f ixed width f ormatted data into a ’data.frame’; widths
match(x, y) returns a vector of the same length than x with the elements
Slicing and extracting data
is an integer vector, giving the widths of the fixed-width fields
of x which are in y (NA otherwise)
save(file,...) saves the specified objects (...) in the XDR platform-
which(x == a) returns a vector of the indices of x if the comparison op-
Indexing vectors
th
independent binary format
eration is true (TRUE), in this example the values of i for which x[i]
element
x[n]
n
save.image(file) saves all objects
th
== a (the argument of this function must be a variable of mode logi-
all but the n
element
x[-n]
cat(..., file="", sep=" ") prints the arguments after coercing to
cal)
first n elements
x[1:n]
character; sep is the character separator between arguments
choose(n, k) computes the combinations of k events among n repetitions
elements from n+1 to the end
x[-(1:n)]
print(a, ...) prints its arguments; generic, meaning it can have differ-
= n!
n
k !k!
x[c(1,4,2)]
specific elements
ent methods for different objects
na.omit(x) suppresses the observations with missing data (NA) (sup-
x["name"]
element named "name"
format(x,...) format an R object for pretty printing
presses the corresponding line if x is a matrix or a data frame)
x[x > 3]
all elements greater than 3
write.table(x,file="",row.names=TRUE,col.names=TRUE,
na.fail(x) returns an error message if x contains at least one NA
all elements between 3 and 5
x[x > 3 & x < 5]
sep=" ") prints x after converting to a data frame; if quote is TRUE,
x[x %in% c("a","and","the")] elements in the given set

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 4