↰ help.wtf

R Reference Card

Free software for statistical computing

Getting help and info

help(topic) or ?topic

Documentation on topic. Special characters need to be quoted, e.g. ?'&&'.

help.search("topic") or ??topic

Search the help system

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)

Show S3 methods of x

methods(class=class(x))

List all the methods to handle objects of class x

findFn()

Search a database of help packages for functions and return a data.frame (requires sos package)

Other R references

CRAN task views

Task views provide summaries of R resources for a given domain. Groups of packages can be installed quickly via the ctv package.

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

Input and output

load()

Loads the dataset written with save

data(x)

Loads specified datasets; if no arg is given it lists all available data sets

read.table(file), read.csv(file), read.delim(“file”), read.fwf(“file”)

Reads a file using defaults sensible for a table/csv/delimited/fixed-width file and creates a data frame from it.

library(x)

Loads add-on packages

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,...)

Formats an R object for pretty printing

write.table(x,file), write.csv(x,file)

Saves x after converting to a data frame

sink(file)

Output to file, until sink()

txtStart and txtStop

Saves a transcript of commands and/or output to a text file (TeachingDemos)

download.file(url)

Downloads url from the net

url.show(url)

Remote input

Database I/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

Clipboard I/O

File connections of functions can also be used to read and write to the clipboard instead of a file.

Mac OS: x <‐ read.delim(pipe(“pbpaste”))

Windows: x <‐ read.delim(ʺclipboardʺ)

See also: read.clipboard (psych)

Packages

install.packages(“pkgs”, lib)

Download and install pkgs from repository (lib) or other external source

update.packages

Checks 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

Indexing vectors

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

Indexing lists

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)

Indexing matrices

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"

X[[ʺnameʺ]]

for data frames

column named "name"

x$name

for data frames

as above (w. partial matching)

Data creation

c(...)

Generic function to combine arguments with the default forming a vector; with recursive=TRUE descends through lists combining all elements into one vector

from:to

Generates a sequence; “:” has operator priority; 1:4 + 1 is “2,3,4,5”

seq(from,to)

Generates a sequence. by= specifies increment; length= specifies desired length

seq(along=x)

Generates 1, 2, ..., length(along); useful in for loops

rep(x,times)

Replicate x times; use each to repeat “each” element of x each times; rep(c(1,2,3),2) is 1 2 3 1 2 3; rep(c(1,2,3),each=2) is 1 1 2 2 3 3

data.frame(...)

Creates a data frame of the named or unnamed arguments, e.g.: data.frame(v=1:4, ch= c("a","B","c","d"), n=10);

Shorter vectors are recycled to the length of the longest

list(...)

Creates a list of the named or unnamed arguments, e.g.: list(a=c(1,2),b="hi", c=3);

array(x,dim=)

Array with data x; specify dimensions like dim=c(3,4,2); elements of x recycle if x is not long enough

matrix(x,nrow,ncol)

Matrix; elements of x recycle

factor(x,levels) encodes a vector x as a factor

gl(n, k, length=n*k, labels=1:n)

Generates levels (factors) by specifying the pattern of their levels; k is the number of levels, and n is the number of replications

expand.grid()

A data frame from all combinations of the supplied vectors or factors

Data conversion

as.array(x), as.character(x), as.data.frame(x), as.factor(x), as.logical(x), as.numeric(x), convert type; for a complete list, use methods(as)