Show Menu
Cheatography

R Programming Cheat Sheet by

Basics

Utility Functions

getwd()
Find the working dir.
setwd("C:/file/path")
Set the working dir.
ls()
List variables
rm(object)
Delete object
str(ob­ject)
Displays internal structure of R object
help.s­tart()
Launch help console
instal­l.p­ack­age­s("p­ack­age­_na­me")
Install package
librar­y(p­ack­age­_name)
Load package
detach­(pa­cka­ge:­pac­kag­e_name)
Remove package
scan()
Read data values

Lists

list(x=1:5, y=c('a', 'b'))
Create list
is.list()
Check if the arg is a list
as.list()
Force the arg to list
lapply­(li­st_­name, function)
Apply function over a list and return as list
sapply­(li­st_­name, function)
Return as suitable data struct­ure­(ve­ctor)

Strings

c("String1","String2")
Create a string vector
toStri­ng(x)
Convert to string
noquto­e(s­tring)
Print string w/o quote
sprintf()
Print text & var values
cat()
Concat­enate & print
touppe­r(s­tring)
Convert to uppercase
tolowe­r(s­tring)
Convert to lowercase
substr­(st­rin­g,n,m)
Extract substrings in a string from n to m
strspl­it(­str­ing­," ")
Split elements of string
paste(­c("a­"­,"b")­,"c")
Concatenate vectors
paste0­(c(­"­a","b­"­),"c­")
Concat w/o separator

Probab­ility Distri­butions

rbinom(n, size, prob)
Binomial
rpois(n, lambda)
Poisson
runif(n, min=0, max=1)
Uniform
rnorm(n, mean=0, sd=1)
Normal
rexp(n, rate=1)
Expone­ntial
 

Vectors

c(2, 4, 6)
Numeric vector
c("one","two","thr")
Character vector
c(TRUE­,FALSE)
Logical vector
rep(1:­2,t­imes=3)
Repeat a vector
rep(1:­2,e­ach=3)
Repeat the elements
which.m­in()
Index of the min
which.m­ax()
Index of the max

Data Frames

data.f­ram­e(x­=1:2,
y=c('a', 'b'))
Create data frame
View(d­f_name)
See full dataframe
head(d­f_name)
See first 6 rows
tail(d­f_name)
See last 6 rows
df_nam­e[cond, ]
Row filter
df_name[c("column")]
Column filter
df_nam­e[c­ond1, ][ ,cond2]
Row and Column filter

Functions

function_name <- function(var){
Do something
return(new_variable)}
args(f­unc­tio­n_name) - Arguments of func
body(f­unc­tio­n_name) - Body of func

Flow Control

If Statement - 
if (condition){
Do something
} else {
Do something different}
-------------------------------
Ifelse Statement -
ifelse(condition, Do something, Do something different)
-------------------------------
Switch Statement -
switch("beta","alpha=1,beta=2,gamma=3,4)

Loops

For Loop -
for (var in sequen­ce){Do something}
While Loop -
while (condi­tion){ Do something}

Visual­iza­tions

barplot()
plot()
qqnorm()
pie()
plot(density())
qplot()
mosaic­plot()
pairs()
boxplot()
hist()
matplot()
ggplot()
 

Arrays

array(­1:24, dim=c(­4,3,2), dimnam­es=......)
Create array with 4 rows, 3 cols and 2 groups

Matrices

m1 <- matrix­(1:12, now=4, ncol=3, dimnam­es=....)
Create a matrix with 4 rows and 3 columns
t(m)
Transpose of matrix
rbind(­m1,m2)
Combine by row
cbind(­m1,m2)
Combine by column
The following applies to arrays also:
dimnam­es(m)
dim(m)
colnam­es(m)
rownam­es(m)
nrow(m)
ncol(m)

Descri­ptive Statistics

summary(object)
Summary of object
class(­object)
Find class of an R object
length­(ob­ject)
Get length of an object
quanti­le(x)
Find quantiles
rowMeans(x)/
colMeans(x)
rowSums(x)/
colSums(x)
table(x)
Build a contin­gency table
descri­be(­object)
Descri­ption of object
subset­(x,­cond)
Create subsets

Hypothesis Testing

t.test­(data, mu=3)
One sample two-sided t-test
t.test­(data, mu=3, altern­ati­ve=­'gr­eater')
One sample one-sided t-test
t.test­(data1, data2, mu=0.5)
Two sample two-sided t-test
t.test(data1, data2, mu=0.5, alternative='less')
Two sample one-sided t-test
t.test­(po­st_­data, pre_data, paired­=TRUE)
Paired test
wilcox.te­st(­data, mu=8, alt='l­ess')
Wilcoxen test
cor.test(data1, data2)
Correlation test
chisq.t­es­t(data)
Chi-square test
ks.tes­t(d­ata1, data2)
If both are frm same distn
shapir­o.t­est­(data)
Normality test
aov(data1~ data2)
ANOVA
lm(data1~ data2)
Regression
 

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

          The Basics of Accounting Cheat Sheet