\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{{[}deleted{]}} \pdfinfo{ /Title (cheat-sheet-r-codes.pdf) /Creator (Cheatography) /Author ({[}deleted{]}) /Subject (- R Codes Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{36A391} \definecolor{LightBackground}{HTML}{F2F9F8} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{- R Codes Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{{[}deleted{]}} via \textcolor{DarkBackground}{\uline{cheatography.com/77271/cs/18982/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}{[}deleted{]} \\ \uline{cheatography.com/deleted-77271} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 28th February, 2019.\\ Updated 28th February, 2019.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{2.58804 cm} x{2.38896 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Libraries to Import}} \tn % Row 0 \SetRowColor{LightBackground} library(readr) & library(dplyr) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} library(ggplot2) & library(broom) \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} library(caret) & library(rpart) \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} library(splines) & library(party) \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} library(leaps) & library(glmnet) \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} library(MASS) & library(class) \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.58804 cm} x{2.38896 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Data Conversion}} \tn % Row 0 \SetRowColor{LightBackground} as.array(x) & as.character(x) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} as.data.frame(x) & as.factor(x) \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} as.logical(x) & as.numeric(x) \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{K-Fold}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{folds \textless{}- crossv\_kfold(data, k = 5)}} {[}k is the number of folds{]}} \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Regularization - Ridge \& Lasso}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textasciicircum{}lambdas\_to\_try \textless{}- 10\textasciicircum{}seq(-3, 5, length.out = 100)\textasciicircum{}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{ridge\_cv \textless{}- cv.glmnet(X, y, alpha = 0, lambda = lambdas\_to\_try,standardize = TRUE, nfolds = 10)}} {[}Setting alpha = 0 implements ridge regression{]}} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{lasso \textless{}- glmnet(data\_x, data\_y, alpha = 1.0)}}} \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Random Forest}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{randomForest(formula, data)}} {[}formula is a formula describing the predictor and response variables. data is the name of the data set used{]}} \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Basic Codes}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{read\_csv("path/nhanes.csv")}} {[}Read nhanes.csv in the path/ folder (readr){]}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{View(df)}} {[}View tabular data frame df in a graphical viewer{]}} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{mean, median, range}} {[}Descriptive stats. Remember na.rm=TRUE if desired{]}} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{filter(df, ..,)}} {[} Filters data frame according to condition ... (dplyr){]}} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{factor(x, levels=c("wt", "mutant"))}} {[}Create factor specifying level order{]}} \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{relevel(x, ref="wildtype")}} {[}Re-level a factor variable{]}} \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{t.test(y\textasciitilde{}grp, data=df)}} {[}T-test mean y across grp in data df{]}} \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{lmfit \textless{}- lm(y\textasciitilde{}x1+x2, data=df)}} {[}Fit linear model y against two x's{]}} \tn % Row Count 16 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{anova(lmfit)}} {[}Print ANOVA table on object returned from lm(){]}} \tn % Row Count 18 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{summary(lmfit)}} {[}Get summary information about a model fit with lm(){]}} \tn % Row Count 20 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{TukeyHSD(aov(lmfit))}} {[}ANOVA Post-hoc pairwise contrasts{]}} \tn % Row Count 22 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{wilcox.test(y\textasciitilde{}grp, data=df)}} {[}Wilcoxon rank sum / Mann-Whitney U test{]}} \tn % Row Count 24 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{xt \textless{}- xtabs(\textasciitilde{}x1+x2, data=df)}} {[}Cross-tabulate a contingency table{]}} \tn % Row Count 26 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{addmargins(xt)}} {[}Adds summary margin to a contingency table xt{]}} \tn % Row Count 28 (+ 2) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{addmargins(xt)}} {[}Adds summary margin to a contingency table xt{]}} \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Basic Codes (cont)}} \tn % Row 15 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{chisq.test(xt)}} {[}Chi-square test on a contingency table xt{]}} \tn % Row Count 2 (+ 2) % Row 16 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{fisher.test(xt)}} {[}Fisher's exact test on a contingency table xt{]}} \tn % Row Count 4 (+ 2) % Row 17 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{mosaicplot(xt)}} {[}Mosaic plot for a contingency table xt{]}} \tn % Row Count 6 (+ 2) % Row 18 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{power.t.test(n, power, sd, delta)}} {[}T-test power calculations{]}} \tn % Row Count 8 (+ 2) % Row 19 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{power.prop.test(n, power, p1, p2)}} {[}Proportions test power calculations{]}} \tn % Row Count 10 (+ 2) % Row 20 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{tidy() augment() glance()}} {[}Model tidying functions in the broom package{]}} \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.58804 cm} x{2.38896 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Data Information}} \tn % Row 0 \SetRowColor{LightBackground} is.na(x) & is.null(x) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} is.nan(x) & is.array(x) \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} is.data.frame(x) & is.numeric(x) \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} is.complex(x) & is.character(x) \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} head(x) & tail(x) \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} summary(x) & str(x) \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} length(x) & dim(x) \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} dimnames(x) & attr(x,which) \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} nrow(x) & ncol(x) \tn % Row Count 9 (+ 1) % Row 9 \SetRowColor{white} NROW(x) & NCOL(x) \tn % Row Count 10 (+ 1) % Row 10 \SetRowColor{LightBackground} class(x) & unclass(x) \tn % Row Count 11 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Data Splitting}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{createDataPartition(y,p=0.8)}} {[}createDaIt splits a vector 'y' with 80 percent data in one part and 20 percent in other parttaPartition(y,p=0.8){]}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{trainControl( summaryFunction=\textless{}Rfunction\textgreater{},classProbs=\textless{}logical\textgreater{})}} {[}It is used for controlling training parameters like resampling, number of folds, iteration etc.{]}} \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{densityplot.rfe(x,data,...)}} {[}Lattice functions for plotting resampling results of recursive feature selection{]}} \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{featureplot(x,y,plot...)}} {[}A shortcut to produce lattice plots{]}} \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}