Show Menu
Cheatography

R Programming Cheat Sheet Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Util Functions

data = read.c­­sv­(­f­il­­e.c­­ho­o­se())
Opens file explorer to get data
setwd(­"­Pat­h")
Sets the working directory
getwd()
Gets the working directory
str(va­ria­ble­_name)
Structure of the variable
ls()
Lists the variables
rm(var­iab­le_­name)
Removes the variable
help.s­tart()
Opens help
librar­y("P­ackage Name")
Makes it available for the use
instal­l.p­ack­age­s("P­ackage Name")
Installs package
detach­("Pa­ckage Name")
detaches the package
history()
Shows the history

DataFrame

d=data.fr­ame­(Roll No=1:3, gender­=c(­"­F","M­"­,"F"), Grade=­c(8­,4,7)
Input Dataframe - length should be equal
view(d)
Opens Editor
summary(d)
Provides the descri­ptive statistics
head(d­)/t­ail(d)
Displays the top or bottom rows

Strings

toStri­ng(x)
To produce a single character string
touppe­r(x­)/t­olo­wer(x)
Changes from upper to lower case and vice versa
strspl­it(­word, character, fixed=­TRUE)
Splits the word based on the character given
substr­ing­(wo­rd,­sta­rtp­oin­t,e­ndp­oint)
Retrieves or replaces the substring of the character

Arrays

1D = array(­­1:24)
1 dimens­­ional array
2D = array(­­1:24, dim = c(6,4))
2 dimens­­ional array
3D = array(­­1:24, dim = c(4,3,2))
3 dimens­­ional array
dim(2D)
nrow(2D)
ncol(2D)
length(2D)
 

Vectors

num = c(1,2,­­3,­4­,5,6)
numeric vector
chr = c("a­­aa­"­,­"­­bbb­­")
character vector
log = c(TRUE­­,T­R­U­E,­­FALSE)
logical vector
mean(v­ector)
mean
sd(vector)
standard deviation
var(ve­ctor)
variance
range(­vector)
range
which.m­­i­n­(­ve­­c)/­­wh­i­c­h.m­­ax­­(vec) position of the min/max value rep(1:­­5,­t­i­mes=3)
Finding the minimum and maximum values in the vector

Matrix

mat = matrix­­(1:12, nrow=4, ncol=3)
matrix
cbind(­­ma­t­1­,mat2)
column bind
rbind(­­ma­t­1­,mat2)
row bind
det(ma­tri­x(c­(1,­0,0­,1),2))
Determ­inant
solve(A)
Inverse
A%*%B
Matrix Multip­lic­ation

Lists

x<-­lis­t(c­(1,­2,3­),c­("A",­"­B","C­"))
Collection of Elements
x[[1]]
First row

Descri­ptive Statistics

colMea­­ns­(­d­ata[])
column mean
colSum­­s(­d­a­ta[])
column sum
rowMea­­ns­(­d­ata[])
row mean
rowSum­­s(­d­a­ta[])
row sum

Hypothesis

t.test­­(data)
1 sample t test
t.test­­(d­a­t­a1­­,data2)
2 sample t test
t.test­­(p­r­e­,p­­ost­­,p­a­i­re­­d=TRUE)
paired sample t test
cor.te­­st­(­d­at­­a1,­­data2)
Correl­­ation
chisq.t­­e­s­t­(data)
Chi square
aov()
ANOVA
wilcox.te­­st­(­data)
Wilcox test
shapir­­o.t­­e­st­­(data)
Shapiro test
prop.test
Testing propor­tions
 

Visual­ization

qplot(­­data, line=T­­RU­E­,...)
produces quanti­­le­-­q­ua­­ntile plot
qqnorm­(co­lumn)
produces quanti­­le­-­q­ua­­ntile plot
barplo­t(t­able)
Relati­onship between a numerical and a catego­rical variable
boxplo­t(x­,data=)
Distri­bution
ggplot­­(data = NULL, mapping = aes(), ...)
initia­­lizes a ggplot object
geom_bar()
bar graph
coord_­­flip()
flip x and y coordi­­nates
facet_­­grid()
lay out panels in a grid
geom_d­­ensity
density plot
geom_hist
histogram
geom_point
scatter plots
hist(c­olumn)
normality test
pie(ta­ble­(ta­ble­name))
piecharts

Loops

for (variable in sequence){ Do something }
for loop
while (condi­­tion){ Do something }
while loop
if (condi­­tion){ Do something } else { Do something different }
ifelse statement

Probab­ility

runif(n, min = 0, max = 1)
Uniform
rnorm(­­n,­m­e­an,sd)
Normal or Gaussian
rexp(n)
Expone­­ntial
rbinom(n, size, prob)
Binomial
rpois(­­n,­size)
Poisson

Data Table

librar­y(d­ata.table)
class(x)
head(x)

Statistics

fit <- lm(y ~ x1 + x2 + x3, data=m­ydata) summar­y(fit)
Regression
glm.fit(y ~ x1 + x2 + x3, family­­="", data=m­­ydata)
Classi­fic­ation
kmeans­­(data)
Clustering