Show Menu
Cheatography

R - Basic Cheat Sheet (DRAFT) by

R programming cheat sheet

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

Util functions

getwd()
Get working directory
setwd()
Set a path in working directory
ls()
list the memory content
rm()
remove variable from memory
instal­l.p­ack­ages()
Installs the required package
library()
Makes the package available for usage
str()
shows the structure of the variable
detach()
removes the package
history()
displays the executed commands in the session
help.s­tart()
opens the index page of R

Data Frame

data_frame = data.f­­ra­m­e­(s­­ubj­­ec­t­I­D=­­1:5­­,g­e­n­de­­r=c­­("M­"­,­"­­F","M­­"­,­"­M",­­"­­F")­,­s­co­­re=­­c(­8­,­3,­­6,5,5))
Data frame with elements of equal length
view(d­ata­_frame)
opens the editor
rbind(­dat­a_f­ram­e1,­dat­a_f­rame2)
combines two data frames vertic­all­y/row wise
cbind(­dat­a_f­ram­e1,­dat­a_f­rame2)
combines two data frames horizo­nta­lly­/column wise
head(d­ata­_fr­ame­)/t­ail­(da­ta_­frame)
returns the first / last part of the data frame
summar­y(d­ata­_frame)
returns the descri­ptive statistics

Strings

toStri­ng(x)
produces a single character string
tolower()
converts text to lower case
toupper()
converts text to upper case
substr()
extract or replace a substring in a character vector
paste (…, sep = " ", collapse = NULL)
concat­enate vectors after character conversion
 

Arrays & Matrices

one_d_­arr­ay<­-ar­ray­(1:15)
one dimens­ional array
two_d_­arr­ay<­-ar­ray­(1:­15,­dim­=c(­3,4))
two dimens­ional array
3_d_ar­ray­<-a­rra­y(1­:15­,di­m=c­(3,­4,2))
three dimens­ional array
mat1=m­atr­ix(­1:1­5,n­row­=5,­ncol=3)
creates a 5x3 matrix
cbind(­mat­1,m­at2­)/r­bin­d(m­at1­,mat2)
column bind/row bind
solve(­mat­1)%­*%mat1
Inverse of a matrix
det(ma­tri­x(c­(1,­0,0­,1),2)
determ­inant of a matrix

Vectors

scores­<-c­(3,­4,6­,7,­5,8,6)
numerical vector
names<­-c(­"­Nan­cy",­"­Sel­var­ani­")
character Vector
x<-­c(T­RUE­,TR­UE,­FALSE)
logical vector
mean(s­cores)
mean of the score vector
sd(Scores)
standard deviation of score vector
var(sc­ores)
variance of the vector
range(­scores)
range of the vector
which.m­in­(ve­cto­r)/­whi­ch.m­ax­(ve­ctor)
returns the position of the min/max value
rep(1:­4,t­imes=2)
replicates the elements of the vector twice

Lists

list<-­lis­t(c­("Na­ncy­", "­sel­var­ani­"­,"Ar­avi­nd")­,c(­25,­23,26))
creates list with elements of diff data types
names(­lis­t)<­-c(­"­Nam­es",­"­Age­")
names the elements in the list

Descri­ptive Statistics

summar­y(m­ydata)
descri­ption of mydata
rowMea­ns(­myd­ata[])
returns the row mean value
colSum­s(m­yda­ta[])
returns the column sum
 

Hypothesis Testing

t.test­(data1)
one sample t test
t.test­(da­ta1­,data2)
two sample t test
t.test­­(p­r­e­,p­­ost­­,p­a­i­re­­d=TRUE)
paired t test
prop.test
test for diff between 2 propor­tions
cor.te­st(­dat­a1,­data2)
correl­ation
wilcox.te­­st­(­d­ata1)
Wilcox test
chisq.t­es­t(d­ata1)
chi square test
shapir­o.t­est­(data1)
test for normality
aov()
ANOVA

Visual­ization

ggplot­­(data = NULL, mapping = aes(), ...)
initia­lizes the plot object
qplot(­­data, line=T­­RU­E­,...)
quanti­le-­qua­ntile plot
geom_d­ens­ity()
density plot
geom_h­ist()
histogram
geom_p­oint()
Scatter plots
geom_bar()
Bar graph
facet_­grid()
panel layout in a grid
barplo­t(list)
bar plot

Probab­ility

rnorm(­n,m­ean,sd)
normal distri­bution
runif()
uniform distri­bution
rpois(­n,size)
poisson distri­bution
rbinom­(n,­siz­e,prob)
Binomial distri­bution
rexp(n)
Expone­ntial distri­bution

Statistics

lm(y~x­,da­ta=­mydata)
linear regression
summar­­y(lm(y ~ x1 + x2 + x3, data=m­­yd­ata))
multiple regression
summar­­y(­glm(y ~ x1 + x2 + x3, family­­="", data=m­­yd­ata))
classi­fic­ation
predic­t(o­bje­ct,­mydata)
Regression model
cl$cluster
Clustering
cluste­r=k­mea­ns(­mydata)
K means cluster analysis