Show Menu

R Basic Cheat Sheet by

programming

Util Functions

getwd()
gets the working directory
setwd(­"­C:/­fil­e/p­ath­")
sets the working directory
data = read.c­sv(­fil­e.c­hoo­se())
opens file explorer to get data
ls()
lists the variables
str(var)
structure of the variable
rm(var)
removes the variable
help.s­tart()
opens help
instal­l.p­ack­age­s("p­ackage name")
installs the package
librar­y("p­ackage name")
makes the contents available to use
detach­("pa­ckage name")
detaches the package

Strings

toStri­ng(x)
helper function to produce a single character string
touppe­r()­/to­lower()
converts text to upper/­lower case
substr­ing­(ch­r,n,n)
retrieve or replaces the substring of the charachter
paste (…, sep = " ", collapse = NULL)
Concat­enate vectors after converting to character

Arrays and Matrix

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
mat = matrix­(1:12, nrow=4, ncol=3)
matrix
cbind(­mat­1,mat2)
column bind
rbind(­mat­1,mat2)
row bind
 

Vector

num = c(1,2,­3,4­,5,6)
numeric vector
chr = c("a­aa",­"­bbb­")
character vector
log = c(TRUE­,TR­UE,­FALSE)
logical vector
mean(vec)
mean
sd(vec)
standard deviation
var(vec)
variance
range(vec)
range
which.m­in­(ve­c)/­whi­ch.m­ax­(vec)
position of the min/max value
rep(1:­5,t­imes=3)
replicate elements of vector

DataFrame

df = data.f­ram­e(s­ubj­ect­ID=­1:5­,ge­nde­r=c­("M",­"­F","M­"­,"M",­"­F"),­sco­re=­c(8­,3,­6,5,5))
dataframe
view(df)
opens editor
head(d­f)/­tai­l(df)
displays top/bottom n rows
summar­y(df)
returns descri­ptive statistics of data

Descri­ptive Statistics

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

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
 

Hypothesis

t.test­(data)
1 sample t test
t.test­(da­ta1­,data2)
2 sample t test
t.test­(pr­e,p­ost­,pa­ire­d=TRUE)
paired sample t test
wilcox.te­st(­data)
Wilcox test
cor.te­st(­dat­a1,­data2)
correl­ation test
chisq.t­es­t(data)
Chi square test
shapir­o.t­est­(data)
Shapiro test
aov()
ANOVA

Visual­ization

qplot(­data, line=T­RUE­,...)
produces quanti­le-­qua­ntile plot
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

Probab­ility

rbinom(n, size, prob)
Binomial distri­bution
rpois(­n,size)
Poisson distri­bution
runif(n, min = 0, max = 1)
Uniform distri­bution
rnorm(­n,m­ean,sd)
Normal distri­bution
rexp(n)
Expone­ntial distri­bution

Statistics

summar­y(lm(y ~ x1 + x2 + x3, data=m­ydata))
multiple regression
summar­y(glm(y ~ x1 + x2 + x3, family­="", data=m­ydata))
classi­fic­ation
cluster = kmeans­(data)
kmeans cluster analysis

Download the R Basic Cheat Sheet

1 Page
//media.cheatography.com/storage/thumb/dipakk_r-basic.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Regular Expressions Cheat Sheet
          PHP Cheat Sheet