Show Menu
Cheatography

Cheat Sheet - R Codes Cheat Sheet by [deleted]

Libraries to Import

librar­y(r­eadr)
librar­y(d­plyr)
librar­y(g­gplot2)
librar­y(b­room)
librar­y(c­aret)
librar­y(r­part)
librar­y(s­plines)
librar­y(p­arty)
librar­y(l­eaps)
librar­y(g­lmnet)
librar­y(MASS)
librar­y(c­lass)

Data Conversion

as.arr­ay(x)
as.cha­rac­ter(x)
as.dat­a.f­rame(x)
as.fac­tor(x)
as.log­ical(x)
as.num­eric(x)

K-Fold

folds <- crossv­_kf­old­(data, k = 5) [k is the number of folds]

Regula­riz­ation - Ridge & Lasso

lambda­s_t­o_try <- 10seq(-3, 5, length.out = 100)^
ridge_cv <- cv.glm­net(X, y, alpha = 0, lambda = lambda­s_t­o_t­ry,­sta­nda­rdize = TRUE, nfolds = 10) [Setting alpha = 0 implements ridge regres­sion]
lasso <- glmnet­(da­ta_x, data_y, alpha = 1.0)

Random Forest

random­For­est­(fo­rmula, data) [formula is a formula describing the predictor and response variables. data is the name of the data set used]
 

Basic Codes

read_c­sv(­"­pat­h/n­han­es.c­sv­") [Read nhanes.csv in the path/ folder (readr)]
View(df) [View tabular data frame df in a graphical viewer]
mean, median, range [Descr­iptive stats. Remember na.rm=TRUE if desired]
filter(df, ..,) [ Filters data frame according to condition ... (dplyr)]
factor(x, levels­=c(­"­wt", "­mut­ant­")) [Create factor specifying level order]
relevel(x, ref="wi­ldt­ype­") [Re-level a factor variable]
t.test­(y~grp, data=df) [T-test mean y across grp in data df]
lmfit <- lm(y~x­1+x2, data=df) [Fit linear model y against two x’s]
anova(­lmfit) [Print ANOVA table on object returned from lm()]
summar­y(l­mfit) [Get summary inform­ation about a model fit with lm()]
TukeyH­SD(­aov­(lm­fit)) [ANOVA Post-hoc pairwise contrasts]
wilcox.te­st(­y~grp, data=df) [Wilcoxon rank sum / Mann-W­hitney U test]
xt <- xtabs(­~x1+x2, data=df) [Cross­-ta­bulate a contin­gency table]
addmar­gin­s(xt) [Adds summary margin to a contin­gency table xt]
addmar­gin­s(xt) [Adds summary margin to a contin­gency table xt]
chisq.t­es­t(xt) [Chi-s­quare test on a contin­gency table xt]
fisher.te­st(xt) [Fisher’s exact test on a contin­gency table xt]
mosaic­plo­t(xt) [Mosaic plot for a contin­gency table xt]
power.t.t­est(n, power, sd, delta) [T-test power calcul­ations]
power.p­ro­p.t­est(n, power, p1, p2) [Propo­rtions test power calcul­ations]
tidy() augment() glance() [Model tidying functions in the broom package]
 

Data Inform­ation

is.na(x)
is.null(x)
is.nan(x)
is.arr­ay(x)
is.dat­a.f­rame(x)
is.num­eric(x)
is.com­plex(x)
is.cha­rac­ter(x)
head(x)
tail(x)
summary(x)
str(x)
length(x)
dim(x)
dimnam­es(x)
attr(x­,which)
nrow(x)
ncol(x)
NROW(x)
NCOL(x)
class(x)
unclass(x)

Data Splitting

create­Dat­aPa­rti­tio­n(y­,p=0.8) [creat­eDaIt splits a vector 'y' with 80 percent data in one part and 20 percent in other partta­Par­tit­ion­(y,­p=0.8)]
trainC­ontrol( summar­yFu­nct­ion­=<R­fun­cti­on>­,cl­ass­Pro­bs=­<lo­gic­al>) [It is used for contro­lling training parameters like resamp­ling, number of folds, iteration etc.]
densit­ypl­ot.r­fe­(x,­dat­a,...) [Lattice functions for plotting resampling results of recursive feature selection]
featur­epl­ot(­x,y­,pl­ot...) [A shortcut to produce lattice plots]
 

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.

          More Cheat Sheets by [deleted]