\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{quantumrustler} \pdfinfo{ /Title (r.pdf) /Creator (Cheatography) /Author (quantumrustler) /Subject (R 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}{000000} \definecolor{LightBackground}{HTML}{F7F7F7} \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 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{quantumrustler} via \textcolor{DarkBackground}{\uline{cheatography.com/212850/cs/46340/}}} \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}quantumrustler \\ \uline{cheatography.com/quantumrustler} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 14th May, 2025.\\ 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*}{2} \begin{tabularx}{8.4cm}{x{4.4 cm} x{3.6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{R Environment}} \tn % Row 0 \SetRowColor{LightBackground} `Ctrl + L` (Windows) & Clear command window \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `ls()` & List objects in environment \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `rm(obj)` & Remove object \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} `print('text')`\{\{nl\}\}`print(obj)` & Displays text or object \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.08 cm} x{3.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Operations and Special Characters}} \tn % Row 0 \SetRowColor{LightBackground} `+, -, *, /, \textasciicircum{}` & Arithmetic operations \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `\%*\%` & Matrix multiplication \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `'` & Transpose \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} `==, !=, \textless{}, \textgreater{}, \textless{}=, \textgreater{}=` & Relational operators \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} `\#` & Comment \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} `\textless{}-` or `=` & Assignment \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Elementary Math Functions}} \tn % Row 0 \SetRowColor{LightBackground} `sqrt(x)` & Square root \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `exp(x=3)` & Exponential of x \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `abs(x=-1)` & Absolute value of x \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} `log(x=exp(1), b=exp(1))` & Logarithm with base b. If b is not specified, e is assumed by default \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Vectors, Matrices, Arrays, Lists, Data Frames}} \tn % Row 0 \SetRowColor{LightBackground} `c(1,2,3)` & Combine values into vector \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `m:n` & Sequence from m to n (can't do spacing) \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} `seq(from=1,to=10,by=2)` & Sequence with step. For decreasing step, by must be -ve \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} `seq(from=3,to=27,length.out=40)` & Sequence with as many numbers specified \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} `rep(x=c(3,62,8.3),times=3,each=2)` & Repeat values. The value for times provides the number of times to repeat x, and each provides the number of times to repeat each element of x. \tn % Row Count 18 (+ 8) % Row 5 \SetRowColor{white} `sort(x=c(2.5,-1,-10,3.44),decreasing=FALSE)` & Sort a vector in increasing or decreasing order \tn % Row Count 21 (+ 3) % Row 6 \SetRowColor{LightBackground} `length(x=c(3,2,8,1))` & Determines how many entries exist in a vector given as the argument x \tn % Row Count 25 (+ 4) % Row 7 \SetRowColor{white} `myvec{[}1{]}\{\{nl\}\}myvec{[}c(1,3,5){]}` & Retrieve specific elements from a vector \tn % Row Count 27 (+ 2) % Row 8 \SetRowColor{LightBackground} `myvec{[}-1{]}\{\{nl\}\}myvec{[}-c(1,3,5){]}` & Delete elements by using negative versions of the indexes \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Vectors, Matrices, Arrays, Lists, Data Frames (cont)}} \tn % Row 9 \SetRowColor{LightBackground} `myvec{[}m:n{]}` & Retrieve elements from a vector with a sequence of indices from m to n \tn % Row Count 4 (+ 4) % Row 10 \SetRowColor{white} `prod(myvec)` & Multiply all elements in a vector \tn % Row Count 6 (+ 2) % Row 11 \SetRowColor{LightBackground} `matrix(data=c(-3,2,893,0.17),nrow=2,ncol=2,byrow=FALSE)` & Create a matrix filled in a column-by-column fashion \tn % Row Count 9 (+ 3) % Row 12 \SetRowColor{white} `rbind(1:3,4:6)` & Bind together vectors as rows of a matrix \tn % Row Count 12 (+ 3) % Row 13 \SetRowColor{LightBackground} `cbind(c(1,4),c(2,5),c(3,6))` & Bind together vectors as columns of a matrix \tn % Row Count 15 (+ 3) % Row 14 \SetRowColor{white} `dim(mymat)`\{\{nl\}\}`nrow(mymat)`\{\{nl\}\}`ncol(mymat)` & Provides the dimensions of a matrix \tn % Row Count 18 (+ 3) % Row 15 \SetRowColor{LightBackground} `A{[},n{]}` & Refers to the elements in all the rows of column n of the matrix A \tn % Row Count 22 (+ 4) % Row 16 \SetRowColor{white} `A{[}n,{]}` & Refers to the elements in all the columns of row n of the matrix A \tn % Row Count 26 (+ 4) % Row 17 \SetRowColor{LightBackground} `A{[},m:n{]}` & Refers to the elements in all the rows between columns m and n of the matrix A \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Vectors, Matrices, Arrays, Lists, Data Frames (cont)}} \tn % Row 18 \SetRowColor{LightBackground} `A(m:n,)` & Refers to the elements in all the columns between rows m and n of the matrix A \tn % Row Count 4 (+ 4) % Row 19 \SetRowColor{white} `A{[}m:n,p:q{]}` & Refers to the elements in rows m through n and columns p through q of the matrix A. \tn % Row Count 9 (+ 5) % Row 20 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Indexing can be done using individual indices in vectors.\{\{nl\}\}To delete or omit elements from a matrix, use negative indexes.} \tn % Row Count 12 (+ 3) % Row 21 \SetRowColor{white} `diag(x=3)` & Create an identity matrix of size 3 x 3 \tn % Row Count 14 (+ 2) % Row 22 \SetRowColor{LightBackground} `diag(x=A)` & Identify the values along the diagonal of a square matrix \tn % Row Count 17 (+ 3) % Row 23 \SetRowColor{white} `t(A)` & Find the transpose of a matrix \tn % Row Count 19 (+ 2) % Row 24 \SetRowColor{LightBackground} `solve(A)` & Find the inverse of a matrix \tn % Row Count 21 (+ 2) % Row 25 \SetRowColor{white} \seqsplit{`list(matrix(data=1:4},nrow=2,ncol=2),c(T,F,T,T),"hello")` & Create a list containing mixed object types. To name the components of a list as it's being created, assign a label to each component in the list command \tn % Row Count 29 (+ 8) % Row 26 \SetRowColor{LightBackground} `lst{[}{[}i{]}{]}` & Access the ith element of a list \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Vectors, Matrices, Arrays, Lists, Data Frames (cont)}} \tn % Row 27 \SetRowColor{LightBackground} `lst{[}1:2{]}` & Returns a sublist of selected elements \tn % Row Count 2 (+ 2) % Row 28 \SetRowColor{white} `names(lst)` & Name list components to make the elements more recognizable and easier to work with \tn % Row Count 7 (+ 5) % Row 29 \SetRowColor{LightBackground} `lst\$name`\{\{nl\}\}`x{[}{[}'name'{]}{]}` & Access element by name (or create new column) \tn % Row Count 10 (+ 3) % Row 30 \SetRowColor{white} `x\$nested \textless{}- list(a=1:3)` & Add a nested list to an existing list \tn % Row Count 12 (+ 2) % Row 31 \SetRowColor{LightBackground} \seqsplit{`data.frame(person=c("Peter"},"Lois","Meg","Chris","Stewie"),\{\{nl\}\}age=c(42,40,17,14,1),\{\{nl\}\}gender=factor(c("M","F","F","M","M")),\{\{nl\}\}stringsAsFactors=TRUE)` & Create a data frame. stringsAsFactors is used to control automatic conversion of character strings to factors \tn % Row Count 20 (+ 8) % Row 32 \SetRowColor{white} `df{[}df\$gender == 'M', {]}` & Logical Subset\{\{nl\}\}Subset rows where gender is M \tn % Row Count 23 (+ 3) % Row 33 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Data frames are treated like matrices,\{\{nl\}\}so you can also use functions like nrow(df).} \tn % Row Count 25 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Non-numeric Values}} \tn % Row 0 \SetRowColor{LightBackground} `TRUE` (or `T`)\{\{nl\}\}`FALSE` (or `F`) & Logical values \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `any(mat)` & Returns TRUE if any of the logicals in the vector are TRUE and returns FALSE otherwise \tn % Row Count 7 (+ 5) % Row 2 \SetRowColor{LightBackground} `all(mat)` & Returns a TRUE only if all of the logicals are TRUE, and returns FALSE otherwise \tn % Row Count 11 (+ 4) % Row 3 \SetRowColor{white} `"This is a character string"` & Character strings \tn % Row Count 13 (+ 2) % Row 4 \SetRowColor{LightBackground} `nchar(x=str)` & Returns the number of characters in a string.\{\{nl\}\}length(x=str) != nchar(x=str) \tn % Row Count 17 (+ 4) % Row 5 \SetRowColor{white} `cat("Hello",\{\{nl\}\}"worldd\textbackslash{}b",\{\{nl\}\}".\textbackslash{}n",\{\{nl\}\}sep=" ")` & Sends output directly to the console screen and doesn't formally return anything \tn % Row Count 22 (+ 5) % Row 6 \SetRowColor{LightBackground} `paste("Hello",\{\{nl\}\}"world",\{\{nl\}\}".",\{\{nl\}\}sep=" ")` & Concatenates and then returns the final character string as a usable R object \tn % Row Count 26 (+ 4) % Row 7 \SetRowColor{white} `substr(x=str,\{\{nl\}\}start=21,\{\{nl\}\}stop=27)` & Extracts a substring from x, starting at start and ending at stop \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Non-numeric Values (cont)}} \tn % Row 8 \SetRowColor{LightBackground} \seqsplit{`sub(pattern="chuck"},\{\{nl\}\}replacement="hurl",\{\{nl\}\}x=str)` & Replaces the first match of pattern in x with replacement \tn % Row Count 3 (+ 3) % Row 9 \SetRowColor{white} \seqsplit{`gsub(pattern="chuck"},\{\{nl\}\}replacement="hurl",\{\{nl\}\}x=bar)` & Replaces all matches of pattern in x with replacement \tn % Row Count 6 (+ 3) % Row 10 \SetRowColor{LightBackground} `factor(x=c("low",\{\{nl\}\}"medium",\{\{nl\}\}"high",\{\{nl\}\}"medium"))` & Converts a vector x into a categorical variable with labeled levels (similar to enums from other languages) \tn % Row Count 12 (+ 6) % Row 11 \SetRowColor{white} `levels(x=myvec)` & Lists the categories (levels) in the factor x \tn % Row Count 15 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Multidimensional Arrays}} \tn % Row 0 \SetRowColor{LightBackground} `array(data=1:24, dim=c(3, 4, 2))` & Creates a 3D array with 3 rows, 4 columns, and 2 layers \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{`array(data=rep(1:24},times=3),dim=c(3,4,2,3))` & Creates a 4D array with dimensions 3×4×2×3 \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} `A{[} , , n{]}` & All rows and columns in the n-th matrix (3rd dim) of A \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} `A{[} , m, n{]}` & All rows in column m of the n-th matrix \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} `A{[}i, , {]}` & All columns and layers of row i \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} `A{[} , , , p{]}` & All rows, columns, and matrices in the p-th 4th dimension slice \tn % Row Count 17 (+ 4) % Row 6 \SetRowColor{LightBackground} `A{[}m:n, , , {]}` & All columns and dimensions for rows m through n \tn % Row Count 20 (+ 3) % Row 7 \SetRowColor{white} `A{[} , , m:n{]}` & All rows and columns for matrices m through n \tn % Row Count 23 (+ 3) % Row 8 \SetRowColor{LightBackground} `A{[}1:2, 2:3, 1, 1{]}` & A specific 2×2 submatrix from layer 1, 4th-dim slice 1 \tn % Row Count 26 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Statistics}} \tn % Row 0 \SetRowColor{LightBackground} `sum(xdata)` & Sum all elements in a vector \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `mean(xdata, na.rm=FALSE)` & Calculates the arithmetic mean \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `median(xdata)` & Finds the median of a data \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `table(xdata)` & Returns the frequencies \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `xtab{[}xtab==max(xtab){]}` & Returns the mode,\{\{nl\}\}where xtab is a table of xdata \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} `min(xdata)` & Returns the smallest value \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} `max(xdata)` & Returns the largest value \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} `range(xdata)` & Returns the smallest and largest values \tn % Row Count 17 (+ 2) % Row 8 \SetRowColor{LightBackground} `round(x, n)` & Round to the specified number of decimal places (n) \tn % Row Count 20 (+ 3) % Row 9 \SetRowColor{white} \seqsplit{`tapply(chickwts\$weight},\{\{nl\}\}INDEX=chickwts\$feed,\{\{nl\}\}FUN=mean)` & Applies mean to the numerical data for each grouping variable \tn % Row Count 24 (+ 4) % Row 10 \SetRowColor{LightBackground} `quantile(x=xdata, prob=0.8`)\{\{nl\}\}`quantile(x=xdata, prob=c(0.25,0.5,0.75)`) & Returns the quantile(s) of interest \tn % Row Count 28 (+ 4) % Row 11 \SetRowColor{white} `summary(xdata)` & Provides statistics automatically \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Statistics (cont)}} \tn % Row 12 \SetRowColor{LightBackground} `var(xdata)`\{\{nl\}\}`sd(xdata)`\{\{nl\}\}`IQR(xdata)` & Direct R commands for computing measures of spread (variance, standard deviation, interquartile range) \tn % Row Count 6 (+ 6) % Row 13 \SetRowColor{white} `cov(xdata,ydata)`\{\{nl\}\}`cor(xdata,ydata)` & Computes the covariance between two numeric vectors\{\{nl\}\}Computes the correlation coefficient between two numeric vectors \tn % Row Count 13 (+ 7) % Row 14 \SetRowColor{LightBackground} `plot(x, y, line="l", xlab="x-axis",ylab="y-axis")` & Creates a scatter plot of y versus x \tn % Row Count 16 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Probability}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Basic Probability Formulas}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Pr(A ∪ B) = Pr(A) + Pr(B) - Pr(A ∩ B)} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Pr(A ∩ B) = 0 & If A and B are disjoint/mutually exclusive (cannot happen at the same time) \tn % Row Count 6 (+ 4) % Row 3 \SetRowColor{white} Pr(A ∩ B) = Pr(A) × Pr(B) & If A and B are independent (not related) \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Pr(A\textasciicircum{}C\textasciicircum{}) = 1 - Pr(A)} \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Pr(A | B) = Pr(A ∩ B) / Pr(B)} \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{P(X \textgreater{} x) = 1 - P(X \textless{}= x)} \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} cumsum(X.prob) & Calculates CDF of discrete RV \tn % Row Count 13 (+ 2) % Row 8 \SetRowColor{LightBackground} \seqsplit{sum(X.prob*X.outcomes)} & Calculates E{[}X{]} (X is discrete RV) \tn % Row Count 15 (+ 2) % Row 9 \SetRowColor{white} sum((X.outcomes{\emph{X.mean)\textasciicircum{}2 }}X.prob)) & Calculates Var(X) (X is discrete RV)\{\{nl\}\}Alternative: E{[}X\textasciicircum{}2\textasciicircum{}{]} - (E{[}X{]})\textasciicircum{}2\textasciicircum{} \tn % Row Count 19 (+ 4) % Row 10 \SetRowColor{LightBackground} F(x) = ∫\textasciicircum{}x\textasciicircum{} f(u) du & CDF (continuous) \tn % Row Count 21 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Plot Probabilities vs. Realizations}}} \tn % Row Count 22 (+ 1) % Row 12 \SetRowColor{LightBackground} \seqsplit{`barplot(height=X.prob},\{\{nl\}\}ylim=c(0,0.5),\{\{nl\}\}names.arg=X.outcomes,\{\{nl\}\}space=0,\{\{nl\}\}xlab="x",\{\{nl\}\}ylab="Pr(X = x)")` & PMF \tn % Row Count 29 (+ 7) % Row 13 \SetRowColor{white} `barplot(X.cumul,\{\{nl\}\}names.arg=X.outcomes,\{\{nl\}\}space=0,\{\{nl\}\}xlab="x",\{\{nl\}\}ylab="Pr(X \textless{}= x)")` & CDF (discrete) \tn % Row Count 34 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Probability (cont)}} \tn % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Common Probability Distributions}}} \tn % Row Count 1 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{X\textasciitilde{}Binomial(size, prob)}} (X is discrete RV)} \tn % Row Count 2 (+ 1) % Row 16 \SetRowColor{LightBackground} dbinom(x=5,size=8,prob=1/6) & Calculates P(X = x) where x is no. of trials \tn % Row Count 5 (+ 3) % Row 17 \SetRowColor{white} sum(dbinom(x=0:5,size=8,prob=1/6))\{\{nl\}\}pbinom(q=5,size=8,prob=1/6) & Calculates P(X \textless{}= q) where x is no. of trials \tn % Row Count 9 (+ 4) % Row 18 \SetRowColor{LightBackground} qbinom(p=0.95,size=8,prob=1/6) & Finds smallest x given P(X \textless{}= x) = p (inverse of CDF) \tn % Row Count 12 (+ 3) % Row 19 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{X\textasciitilde{}Pois(λ)}} (X is discrete RV)} \tn % Row Count 13 (+ 1) % Row 20 \SetRowColor{LightBackground} dpois(x=3,lambda=3.22) & Calculates P(X = x) where x is no. of events observed \tn % Row Count 16 (+ 3) % Row 21 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{Tip:}} P(X = 5) is meaningless so\{\{nl\}\}P(X \textless{} 5) = P(X \textless{}= 5)} \tn % Row Count 18 (+ 2) % Row 22 \SetRowColor{LightBackground} ppois(q=3,lambda=3.22) & Calculates P(X \textless{}= q) where q is no. of events \tn % Row Count 21 (+ 3) % Row 23 \SetRowColor{white} qpois(p=0.95,lambda=3.22) & Finds smallest x given P(X \textless{}= x) = p (inverse of CDF) \tn % Row Count 24 (+ 3) % Row 24 \SetRowColor{LightBackground} rpois(n=15,lambda=3.22) & Generates n random numbers from a Poisson distribution given lambda \tn % Row Count 28 (+ 4) % Row 25 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{X\textasciitilde{}Normal(μ, σ)}} (X is continuous RV)} \tn % Row Count 29 (+ 1) % Row 26 \SetRowColor{LightBackground} dnorm(x, mean, sd) & Returns the height of the normal distribution curve at x \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Probability (cont)}} \tn % Row 27 \SetRowColor{LightBackground} pnorm(q, mean, sd)\{\{nl\}\}Default: μ = 0, σ = 1 & Calculates P(X \textless{}= q) given μ and σ\{\{nl\}\}or P(Z \textless{}= z) if defaults are used \tn % Row Count 4 (+ 4) % Row 28 \SetRowColor{white} qnorm(p, mean, sd) & Finds smallest x given P(X \textless{}= x) = p (inverse of CDF) \tn % Row Count 7 (+ 3) % Row 29 \SetRowColor{LightBackground} qnorm(p, lower.tail=FALSE) & Finds smallest z given P(Z \textgreater{} z) = p\{\{nl\}\}Equal to P(Z \textless{}= z) = 1 - p \tn % Row Count 11 (+ 4) % Row 30 \SetRowColor{white} rnorm(n, mean, sd) & Generates n random numbers from a Normal distribution given μ and σ \tn % Row Count 15 (+ 4) % Row 31 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{QQ Plots and Histograms}}} \tn % Row Count 16 (+ 1) % Row 32 \SetRowColor{white} \seqsplit{hist(chickwts\$weight}, main="", xlab="weight", xlim=c(xi,xf)) & Draws a histogram of the given data \tn % Row Count 19 (+ 3) % Row 33 \SetRowColor{LightBackground} \seqsplit{qqnorm(chickwts\$weight}, main="Normal QQ plot of weights") & Creates a QQ plot \tn % Row Count 22 (+ 3) % Row 34 \SetRowColor{white} \seqsplit{qqline(chickwts\$weight}, col="gray") & Adds a reference line to the QQ plot \tn % Row Count 24 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}