RReferenceCard2.0
Public domain, v2.0 2012-12-24.
V 2 by Matt Baggott, matt@baggott.net
V 1 by Tom Short, t.short@ieee.org
Material from R for Beginners by permission of
Emmanuel Paradis.
Gettinghelpandinfo
help(topic)documentation on topic
?topic same as above; special chars need quotes: for
example ?’&&’
help.search(ʺtopicʺ)search the help system; same
as ??topic
apropos(ʺtopicʺ) the names of all objects in the
search list matching the regular expression
“topic”
help.start() start the HTML version of help
summary(x) generic function to give a “summary”
of x, often a statistical one
str(x) display the internal structure of an R object
ls() show objects in the search path; specify
pat="pat" to search on a pattern
ls.str()str for each variable in the search path
dir() show files in the current directory
methods(x) shows S3 methods of x
methods(class=class(x)) lists all the methods to
handle objects of class x
findFn()searches a database of help packages for
functions and returns a data.frame (sos)
OtherRReferences
CRANtaskviews are summaries of R resources for
task domains at: cran.r-project.org/web/views
Can be accessed via ctv package
RFAQ:cran.r-project.org/doc/FAQ/R-FAQ.html
RFunctionsforRegressionAnalysis, by Vito
Ricci: cran.r-project.org/doc/contrib/Ricci-
refcard-regression.pdf
RFunctionsforTimeSeriesAnalysis, by Vito
Ricci: cran.r-project.org/doc/contrib/Ricci-
refcard-ts.pdf
RReferenceCardforDataMining, by Yanchang
Zhao: www.rdatamining.com/docs/R-refcard-
data-mining.pdf
RReferenceCard, by Jonathan Baron: cran.r-
project.org/doc/contrib/refcard.pdf
Operators
<‐ Left assignment, binary
‐> Right assignment, binary
= Left assignment, but not recommended
<<‐ Left assignment in outer lexical scope; not
for beginners
$ List subset, binary
‐ Minus, can be unary or binary
+ Plus, can be unary or binary
~ Tilde, used for model formulae
: Sequence, binary (in model formulae:
interaction)
:: Refer to function in a package, i.e,
pkg::function; usually not needed
* Multiplication, binary
/ Division, binary
^ Exponentiation, binary
%x% Special binary operators, x can be
replaced by any valid name
%% Modulus, binary
%/% Integer divide, binary
%*% Matrix product, binary
%o% Outer product, binary
%x% Kronecker product, binary
%in% Matching operator, binary (in model
formulae: nesting)
!x logical negation, NOT x
x&y elementwise logical AND
x&&y vector logical AND
x|y elementwise logical OR
x||y vector logical OR
xor(x,y) elementwise exclusive OR
< Less than, binary
> Greater than, binary
== Equal to, binary
>= Greater than or equal to, binary
<= Less than or equal to, binary
Packages
install.packages(“pkgs”,lib)download and install
pkgs from repository (lib) or other external
source
update.packageschecks for new versions and
offers to install
library(pkg) loads pkg, if pkg is omitted it lists
packages
detach(ʺpackage:pkgʺ) removes pkg from memory
Indexingvectors
x[n] nth element
x[‐n] all but the nth element
x[1:n] first n elements
x[‐(1:n)] elements from n+1 to end
x[c(1,4,2)] specific elements
x[ʺnameʺ] element named "name"
x[x>3] all elements greater than 3
x[x>3&x<5] all elements between 3 and 5
x[x%in%c(ʺaʺ,ʺifʺ)]elements in the given set
Indexinglists
x[n] list with elements n
x[[n]] nth element of the list
x[[ʺnameʺ]] element named "name"
x$name as above (w. partial matching)
Indexingmatrices
x[i,j] element at row i, column j
x[i,] row i
x[,j] column j
x[,c(1,3)] columns 1 and 3
x[ʺnameʺ,] row named "name"
Indexingmatricesdataframes(sameasmatrices
plusthefollowing)
X[[ʺnameʺ]] column named "name"
x$name as above (w. partial matching)
Inputandoutput(I/O)
RdataobjectI/O
data(x)loads specified data set; if no arg is given it
lists all available data sets
save(file,...)saves the specified objects (...) in XDR
platform-independent binary format
save.image(file) saves all objects
load(file) load datasets written with save
DatabaseI/O
Useful packages: DBI interface between R and
relational DBMS; RJDBC access to databases
through the JDBC interface; RMySQL interface to
MySQL database; RODBC ODBC database access;
ROracle Oracle database interface driver; RpgSQL
interface to PostgreSQL database; RSQLite SQLite
interface for R