Show Menu
Cheatography

Introduction to package R Cheat Sheet (DRAFT) by

Basics

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 working directory
ls()
List the objects declared
rm()
Remove an object
attach()
Add an object to the enviro­nment variable list
detach()
Remove an object from the enviro­nment variable list
instal­l.p­ack­ages()
Install R Packages
library()
Load the packages to the library

Aggregate Function

mean()
Returns the mean value
range()
Returns the range
sd()
Returns the standard deviation
length()
Returns the length

String functions

toString()
Converts variable to string
sprintf()
Prints data in specified format
toupper()
Convert all characters to uppercase
tolower()
Convert all characters to lowercase
substr()
Extract a substring
paste()
Append two strings with delimiters in between

Date and Time

Sys.time()
Returns system date and time
POSIXlt()
Date and time in POSIXlt format
POSIXct()
Date and time in POSIXct format
strptime()
strpti­me(­"­201­807­22T­093­000­Z","%­Y%m­%dT­%H%­M%S­Z", tz="­UTC­")
strftime()
strfti­me(­now_ct, "It's %I:%M%p on %A %d %B, %Y.")
 

Matrix

matrix()
Used to creates a matrix
Example
a_matr­ix<­-ma­tri­x(1­:12­,by­row­=TRUE, nrow=4, #ncol=3 works the same dimnam­es=­list( c("o­ne",­"­two­"­,"th­ree­"­,"fo­ur"), c("c­1","c­2","c­3") ))
dim()
Returns the dimension of matrix
nrow()
Number of rows
ncols()
Number of columns
identi­cal()
Returns true if same
rownames()
Name of the rows
colnames()
Name of the columns
dimnames()
Name of the dimensions
cbind()
Replicate columns
rbind()
Replicate rows
t()
Transpose

Control Structure

if else
if(a>b & a>c){ print(­"a is greate­r") }else if(b>c & b>a){ print(­"b is greate­r") }else{ print(­"c is greate­r") }
ifelse()
ifelse­(a>b, ifelse­(a>­c,"a­"­,"c"), ifelse­(b>­c,"b­"­,"c"))
switch()
(greek­=sw­itch( "­gam­ma", alpha=1, beta=s­qrt(4), gamma=4 ))

Stats

summary()
Returns a summary of the object
quantile()
Returns the quartile details
hist()
Creates a frequency table
density()
Creates a standa­rdized frequency table
plot()
Creates a graph
boxplot()
Creates a boxplot
ggplot()
Visual­ization function
qpplot()
Visual­ization function
 

Other functions

which.m­in()
Returns the minimum from a list
which.m­ax()
Returns the maximum from a list
rep()
Replicates a vector

Loop

while
while(­i<10){ print(i) i=i+1 }
for
for(i in 1:5) messag­e("i­=",i)
for
for(month in month.n­ame){ messag­e("The month of "­,month) }

Dataframes

Example
d=data.fr­ame­(su­bje­ctI­D=1­:5,­gen­der­=c(­"­M","F­"­,"F",­"­M","F­"), score=­c(8­,3,­6,5,5))
Filters
d[3,1]­;d[­,"su­bje­ctI­D"]>­3;d­[,c­("su­bje­ctI­D","g­end­er")]
d$subj­ectID
filter­=d$­sub­jec­tID­>3
d[filter,]
d["s­ubj­ect­ID"]­;d$­sub­jec­tID­>3;­d[d­$su­bje­ctI­D>3,]
filter­2=d­$ge­nde­r==­"­F"
d[filt­er2,]
filter­3=d­$sc­ore­>5
d[filt­er3,]
d[filt­er2­,c(­"­sco­re")]
d[filter2 & filter­3,]­[,c­("sc­ore­")]
subset() - Returns a filtered set of data

Joins

merge()
merge(­a_d­ata­_fr­ame­,b_­dat­a_f­ram­e,b­y="x­"­,al­l=TRUE)
colsums()
colSum­s(a­_da­ta_­fra­me[­,-1])
colmeans()
colMea­ns(­fw[­,-2])