R Cheat Sheets

ADVERTISEMENT

R Cheat Sheet
Basic Syntax
Example
#
# This is not interpretted
Comments
<- or =
a <- 1; b = 2
Assignment
<<-
a <<- 10
# Not recommended
Global Assignment
v[1]
v[1]
First element in a vector
*
c(1,1)*c(1,1)
# 1 1
Scalar Multiplication
%*%
c(1,1)%*%c(1,1)
# 2
Matrix Multiplication
/
1/2
# 0.5
Division
%/%
1%/%2
# 0
Integer Division
%%
7%%6
# 1
Remainder
Vector and Matrix Operations
Construction
c()
v <- c(1,2,3,4)
# 1 2 3 4
Concatenate
cbind()
cbind(v,v)
# 2 Columns
Column Concatenate
rbind()
rbind(v,v)
# 2 Rows
Row Concatenate
matrix()
mat <- matrix(v,nrow=2,ncol=2)
Create matrix
Selection
v[1]
Select first
tail(v,1)
Select last
mat[2,1]
Select row 2, column 1
mat[1,]
Select row 1
mat[,2]
Select column 2
v[c(1,3)]
Select the first and third values
v[-c(1,3)]
Select all but the first and third values
mat[,c(1,2)]
Select columns 1 and 2
mat[,1:5]
Select columns 1 to 5
mat[“col”]
Select column named “col”
Utility
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
1

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 4