Here is my own R Reference card or cheatsheet which i ve been using in my MSc and other data science studies.

 

Getting help and info

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)

 

 

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

 

 

 

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”

Indexing matrices data frames (same as matrices plus the following)
X[[ʺnameʺ]] column named “name”
x$name as above (w. partial matching)