Show Menu

R Programming Cheat Sheet by

Basics
basics

Utility Functions

getw­d()
Find the working dir.
setwd("C:/file/path")
Set the working dir.
ls()
List variables
rm(o­bje­ct)
Delete object
str(­obj­ect)
Displays internal structure of R object
help.st­art()
Launch help console
inst­all.pa­cka­ges­("pa­cka­ge_­nam­e")
Install package
libr­ary­(pa­cka­ge_­name)
Load package
deta­ch(­pac­kag­e:p­ack­age­_na­me)
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
lapp­ly(­lis­t_name, function)
Apply function over a list and return as list
sapp­ly(­lis­t_name, function)
Return as suitable data struct­ure­(ve­ctor)

Strings

c("String1","String2")
Create a string vector
toSt­rin­g(x)
Convert to string
noqu­toe­(st­ring)
Print string w/o quote
spri­ntf()
Print text & var values
cat()
Concat­enate & print
toup­per­(st­ring)
Convert to uppercase
tolo­wer­(st­ring)
Convert to lowercase
subs­tr(­str­ing­,n,m)
Extract substrings in a string from n to m
strs­pli­t(s­tri­ng,­" ")
Split elements of string
paste(c("a","b"),"c")
Concatenate vectors
paste0(c("a","b"),"c")
Concat w/o separator

Probab­ility Distri­butions

rbin­om(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(TR­UE,­FAL­SE)
Logical vector
rep(­1:2­,ti­mes­=3)
Repeat a vector
rep(­1:2­,ea­ch=3)
Repeat the elements
which.min()
Index of the min
which.max()
Index of the max

Data Frames

data.fr­ame­(x=1:2,
y=c('a', 'b'))
Create data frame
View­(df­_na­me)
See full dataframe
head­(df­_na­me)
See first 6 rows
tail­(df­_na­me)
See last 6 rows
df_n­ame­[cond, ]
Row filter
df_name[c("column")]
Column filter
df_n­ame­[cond1, ][ ,cond2]
Row and Column filter

Functions

function_name <- function(var){
Do something
return(new_variable)}
args­(fu­nct­ion­_na­me) - Arguments of func
body­(fu­nct­ion­_na­me) - 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 someth­ing}
While Loop -
while (condi­tion){ Do someth­ing}

Visual­iza­tions

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

Arrays

arra­y(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, dimnames=....)
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:
dimn­ame­s(m)
dim(m)
colnames(m)
rown­ame­s(m)
nrow(m)
ncol(m)

Descri­ptive Statistics

summary(object)
Summary of object
clas­s(o­bje­ct)
Find class of an R object
leng­th(­obj­ect)
Get length of an object
quan­til­e(x)
Find quantiles
rowMeans(x)/
colMeans(x)
rowSums(x)/
colSums(x)
tabl­e(x)
Build a contin­gency table
desc­rib­e(o­bje­ct)
Descri­ption of object
subs­et(­x,c­ond)
Create subsets

Hypothesis Testing

t.te­st(­data, mu=3)
One sample two-sided t-test
t.te­st(­data, mu=3, alternative='greater')
One sample one-sided t-test
t.te­st(­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.te­st(­pos­t_data, pre_data, paired=TRUE)
Paired test
wilc­ox.t­es­t(data, mu=8, alt='less')
Wilcoxen test
cor.test(data1, data2)
Correlation test
chisq.test(data)
Chi-square test
ks.t­est­(data1, data2)
If both are frm same distn
shapiro.test(data)
Normality test
aov(­data1~ data2)
ANOVA
lm(data1~ data2)
Regression

Download the R Programming Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/ann-santhosh_r-programming.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

          Extending Ruby with C - Part 1 Cheat Sheet