# R Command Cheat Sheet

R Cheatsheet
Notes:
1. This is by no means a comprehensive list, as a large number of useful functions have been left out, and not all options for
the functions listed have been given. This list is purely intented to give a place to begin, as I remember how frustrating it
was to not even know what to start looking for!
2. Typing ?functionname at the command line brings up a help window for the function name listed.
3. Assume in the examples that all vectors and matrices (vi’s and mati’s) have been created.
Command
Example
Result
Operators
General
<-
Assignment operator (suggested)
ans1 <- 1
1
=
Assignment operator
ans2 = 1+1
2
#
Comment
#This is a comment
Mathematical
+
2.5+ans3
5.5
-
Subtraction
ans3-2.5
0.5
*
Scalar multiplication
2*3
6
/
Division operator
6/2
3
^
Exponentiation
2^3
8
Logical/Relational
==
Equals
ans3==3
TRUE
!=
Not Equal
ans3!=3
FALSE
>
Greater Than
ans3>3
FALSE
>=
Greater Than or Equal To
ans3>=3
TRUE
<
Less Than
ans3<3
FALSE
<=
Less Than or Equal To
ans3<=3
TRUE
||
Or
ans1==2 || ans2==2
TRUE
|
Or (use with vectors and matrices)
v2[v1==3 | v1==4]
{3,5}
&&
And
ans1==2 && ans2==2
FALSE
&
And (use with vectors and matrices)
v2[v1==3 & v1==4]
{NA}
%*%
Matrix multiplication
mat1%*%mat1
Functions
sqrt
Square root
sqrt(16)
4
exp
Exponentiation
exp(1)
2.718282
log
Natural log
log(2.718282)
1
sum
Sum
sum(2,3,4)
9
prod
Product
prod(2,3,4)
24
ceiling
Smallest integer ≥number
ceiling(2.1)
2
floor
Integer part of a number
floor(2.1)
2
abs
Absolute value
abs(-0.2)
0.2
sin
Sine
sin(pi/2)
1
cos
Cosine
cos(pi)
-1
tan
Tangent
tan(pi/4)
1
table
Calculate frequency counts of a vector table(v4)
1 3 5
[3 3 3]
Vector/Matrix Functions
Vector creation functions
c
Concatenate
v1 <- c(2,3,4)
2,3,4
v2 <- c(1,3,5)
1,3,5
seq
Sequence
v3 <- seq(from=2, to=10, by=2) 2,4,6,8,10
seq(from=2, to=4, length=5)
2.0,2.5,3.0,3.5,4.0
:
Integer sequence
2:10
2,3,4,5,6,7,8,9,10
rep
Repeat
v4 <- rep(v2, 3)
1,3,5,1,3,5,1,3,5