You are on page 1of 2

R Cheat Sheet

Basic Syntax

The following is a list of symbols that can be used in R. Please review the table below
for specific description and examples related to the listed syntaxes.

Symbol Description Example


# Comments # This is not interpreted
<- or = Assignment a <- 1; b = 2
<<- Global Assignment a <<- 10 # Not recommended
v[1] First element in a vector v[1]
* Scalar Multiplication c(1,1)*c(1,1) # 1 1
%*% Matrix Multiplication c(1,1)%*%c(1,1) # 2
/ Division 1/2 # 0.5
%/% Integer Division 1%/%2 # 0
%% Remainder 7%%6 # 1

Vector and Matrix Operations

Vectors can be used for concatenating rows and columns. Please review the table
below for examples related to vector and matrix operations in R.

Symbol Description Example


c() Concatenate v <- c(1,2,3,4) # 1 2 3 4
cbind() Column Concatenate cbind(v,v) # 2 Columns
rbind() Row Concatenate rbind(v,v) # 2 Rows
matrix() Create matrix mat <- matrix(v,nrow=2,ncol=2)
Selection

The following is a list of symbols that can be used in R. Please review the table below
for specific description and examples related to the listed syntaxes that can be used for
various selections.

Symbol Description Example


v[1] Select first # This is not interpreted
tail(v,1) Select last a <- 1; b = 2
mat[2,1] Select row 2, column 1 a <<- 10 # Not recommended
mat[1,] Select row 1 v[1]
mat[,2] Select column 2 c(1,1)*c(1,1) # 1 1
v[c(1,3)] Select the first and third values c(1,1)%*%c(1,1) # 2
v[-c(1,3)] Select all but the first and third values 1/2 # 0.5
mat[,c(1,2)] Select columns 1 and 2 1%/%2 # 0
mat[,1:5] Select columns 1 to 5 7%%6 # 1

Utility

Please review the table below for a list of most commonly used utility functions in R.

Symbol Description
length() Length of vector
dim() Dimensions of vector/matrix/dataframe
sort() Sorts vector
order() Index to sort vector e.g. sort(v) == v[order(v)]
names() Names of columns

You might also like