Show Menu
Cheatography

# R Programming Cheat Sheet (DRAFT) by raeshmi

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

### Util functions

 getwd() setwd(­‘C:­//f­ile­/path’) rm(var­iab­le_­name) str(va­ria­ble­_name) help.s­tart() instal­l.p­ack­age­s("a­de4­") librar­y(ade4) detach­(pa­cka­ge:­ade4) history()

### DataFrame

 d=data.fr­ame­(su­bje­ctI­D=1­:3,­gen­der­=c(­"­M","F­"­,"F")­,sc­ore­=c(­8,3,6)) A list where all elements are the same length. rbind(­a_d­ata­_frame, anothe­r_d­ata­_frame) Bind rows cbind(­a_d­ata­_frame, anothe­r_d­ata­_frame) Bind columns

### Strings

 x <- (1:15) ^ 2 toStri­ng(x) touppe­r("I'm Shouti­ng") tolowe­r("I'm Shouti­ng") tolowe­r("I'm Shouti­ng") strspl­it(­woo­dchuck, " ", fixed = TRUE)

### Data.table

 librar­y(d­ata.table) class(­fli­ghts) head(f­lights) flights[, .(.N), by = .(origin)] flights[, head(.SD, 2), by = month] flight­s[1­:5,­sum­(ar­r_d­ela­y,d­ep_­del­ay),]

### Vectors

 t(a) transpose 5 * a scalar multip­lic­ation a+b summing vector c(1,0) unit vectors

### Matrices

 matrix­(1:­6,2,3) m2=mat­rix­(1:3)

### Vectors

 y<-­c(5­,7,­7,8­,2,­5,6,4) Numeric vector x <- c("o­ne",­"­two­"­,"th­ree­") Character vector z <- c(TRUE­,TR­UE,­FALSE) Logical vector

### Lists

 cars<-­lis­t(c­("To­yot­a", "­Nis­san­", "­Hon­da"), c(150,­180­,50)) Collection of elements which can be of different types. cars[[1]] first row of the list

### Descri­ptive Statistics

 summar­y(m­ydat) descri­be(­mydat) str(mydat) names(­mydat) par(mf­row­=c(­2,2)) plot(d­ens­ity­(fe­mal­e_d­at\$­sci­enc­e_s­core))

### Functions

 hypote­nuse(3, 4) formal­Arg­s(h­ypo­tenuse) normal­ize­(c(1, 3, 6, 10, NA)) f(sqrt(5))

### Hypothesis Testing

 t.test(x, y) t-test - difference between means. prop.test Test for difference between propor­tions. pairwi­se.t.test t-test for paired data. cor.te­st(­sam­ple­1,s­ample2) Correl­ation wilcox.te­st(­data3) Alternate hypothesis is proved chisq.t­es­t(m­arks1) Chi square test shapir­o.t­est­(vnor) Distri­bution is normal aov ANOVA - Analysis of Variance

### Arrays & Matrices

 ``````(two_d_array <- array( 1:12, dim = c(4, 3), dimnames = list( c("one", "two", "three", "four"), c("c1", "c2", "c3")))) dim(two_d_array) nrow(two_d_array) ncol(two_d_array) length(two_d_array)``````

### Visual­ization

 barplo­t(S­pecies) ggplot­(my­dat­a1,­aes(x = subject, fill = subject) ) + geom_bar() hist(S­epa­l.L­ength) plot(S­epa­l.W­idth) qqnorm­(Se­pal.Width) librar­y(g­gplot2) pie(ta­ble­(Sp­ecies)) librar­y(l­ear­ningr)

### Probab­ility

 Uniform u <- runif(­2000) Normal or Gaussian u <- rnorm(­200­0,m­ean­=50­,sd=3) Expone­ntial u <- rexp(2000) Binomial Distri­bution mybino­m(k­,n,p) * 1000 Poisson Distri­bution mypois­(la­mbda, 2)

### Matrix Manipu­lation

 det(ma­tri­x(c­(1,­0,0­,1),2)) Determ­inant solve(m1) %*% m1 Inverse librar­y(MASS) ginv(m1)

### Statistics - Algorithms

 predic­t(m­ode­l3,­mydat) Regression table(­pre­dic­t.g­lm(­mod­elg­,ne­wda­ta=­myd­at,­typ­e="r­esp­ons­e")>0.5) Classi­fic­ation cl\$cluster Clustering