\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{Niki (worlddoit)} \pdfinfo{ /Title (r-part6-1.pdf) /Creator (Cheatography) /Author (Niki (worlddoit)) /Subject (r part6.1 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}{566BA3} \definecolor{LightBackground}{HTML}{F4F5F9} \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 part6.1 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Niki (worlddoit)} via \textcolor{DarkBackground}{\uline{cheatography.com/170195/cs/36504/}}} \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}Niki (worlddoit) \\ \uline{cheatography.com/worlddoit} \\ \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 21st January, 2023.\\ 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Introduction}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{R is not a fully functional programming language because its functions are not pure, however it allows for a lot of functional programming by means of higher-order-functions \newline % Row Count 4 (+ 4) {\bf{Higher-order-functions:}} \newline % Row Count 5 (+ 1) \{\{fa-check-square\}\} Functions: take vectors as input and return vectors as output \newline % Row Count 7 (+ 2) \{\{fa-check-square\}\} Functionals: take functions (and vectors) as input and return vectors as output. They allow to generalize and reapply techniques in data analysis to any number of inputs \newline % Row Count 11 (+ 4) \{\{fa-check-square\}\} Function Factories: take vectors as input and return functions as output. These are used to create functions \newline % Row Count 14 (+ 3) \{\{fa-check-square\}\} Function Operators: take functions as input and return functions as output. They are used to modify the behavior of functions% Row Count 17 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(**Basics)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Functionals}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{A functional is a function that takes functions as input and returns vectors as output. \newline % Row Count 2 (+ 2) `integrate(cos, 0, pi)`\{\{noshy\}\} \newline % Row Count 3 (+ 1) `4.922505e-17 with absolute error \textless{} 2.2e-14`\{\{noshy\}\} \newline % Row Count 5 (+ 2) They are a common alternative to for loops. \newline % Row Count 6 (+ 1) The basic syntax is `map(.x, .f, ...)` where\{\{noshy\}\} \newline % Row Count 8 (+ 2) `.x` can be a list or any atomic vector\{\{noshy\}\} \newline % Row Count 9 (+ 1) `.f` is a function that will be applied to each element of `.x`.\{\{noshy\}\} \newline % Row Count 11 (+ 2) The code of `map` is very simple:\{\{noshy\}\} \newline % Row Count 12 (+ 1) `simple\_map \textless{}- function(x, f, ...) \{`\{\{noshy\}\} \newline % Row Count 13 (+ 1) ` out \textless{}- vector("list", length(x))`\{\{noshy\}\} \newline % Row Count 14 (+ 1) ` for (i in seq\_along(x)) \{`\{\{noshy\}\} \newline % Row Count 15 (+ 1) `out{[}{[}i{]}{]} \textless{}- f(x{[}{[}i{]}{]}, ...) \}`\{\{noshy\}\} \newline % Row Count 16 (+ 1) `out\}`\{\{noshy\}\} \newline % Row Count 17 (+ 1) In `purrr` there is a special syntax for anonymous functions\{\{noshy\}\} \newline % Row Count 19 (+ 2) `\textgreater{} purrr::map\_dbl(mtcars, function(x) length(unique(x)))`\{\{noshy\}\} \newline % Row Count 21 (+ 2) is similar to \newline % Row Count 22 (+ 1) `\textgreater{} purrr::map\_dbl(mtcars, ∼ length(unique(.x)))`\{\{noshy\}\} \newline % Row Count 24 (+ 2) The `map` extended family and friends is quite large:\{\{noshy\}\} \newline % Row Count 26 (+ 2) \{\{popup="https://adv-r.hadley.nz/functionals.html"\}\}details.\{\{/popup\}\} \newline % Row Count 28 (+ 2) If all you want is to substitute the `for` loop, than the `base`\{\{noshy\}\} \newline % Row Count 30 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Functionals (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{functionals of the `apply` family might be a better choice.\{\{noshy\}\} \newline % Row Count 2 (+ 2) `\textgreater{} x \textless{}- lapply(1:100, sqrt)`\{\{noshy\}\} \newline % Row Count 3 (+ 1) `\textgreater{} apply(mtcars, 2, mean) \#margin = 1 by row, 2 by col`\{\{noshy\}\} \newline % Row Count 5 (+ 2) If you can substitute the for loop with vectorization no need to \newline % Row Count 7 (+ 2) use functionals.% Row Count 8 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(**Basics)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Functionals - Time Example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} (benchmap\textless{}-bench::mark(\{\{noshy\}\} \newline + forloop = \{ x \textless{}- vector("list",100)\{\{noshy\}\} \newline + for(i in seq\_along(100)) x{[}{[}i{]}{]}\textless{}-sqrt(i)\},\{\{noshy\}\} \newline + lapp = \{x \textless{}- lapply(1:100, sqrt)\}, \newline + mappurrr = \{ x \textless{}- purrr::map(1:100,sqrt)\},\{\{noshy\}\} \newline + check = F\{\{noshy\}\} \newline + ))\{\{noshy\}\} \newline \# A tibble: 3 x 13\{\{noshy\}\} \newline expression min median itr/sec\{\{noshy\}\} \newline \textless{}bch:expr\textgreater{} \textless{}bch:tm\textgreater{} \textless{}bch:tm\textgreater{} \textless{}dbl\textgreater{}\{\{noshy\}\} \newline 1 forloop 1.44ms 1.57ms 580.\{\{noshy\}\} \newline 2 lapp 34.64us 36.47us 25570.\{\{noshy\}\} \newline 3 mappurrr 176.5us 188.17us 5054.\{\{noshy\}\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(***Advanced)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Functionals - Time Example photo}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/worlddoit_1673626969_ экрана 2023-01-13 в 17.21.09.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(***Advanced)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{The apply family}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{fa-bolt\}\} `apply(X, MARGIN, FUN, ...)` returns a vector or array or list of values obtained by applying \{\{noshy\}\}a function to margins of an array or matrix. \newline % Row Count 4 (+ 4) `apply(mat, 1, sum)`\{\{noshy\}\} \newline % Row Count 5 (+ 1) \{\{fa-bolt\}\} `lapply` returns a list, each element of which is the result of\{\{noshy\}\} \newline % Row Count 7 (+ 2) applying a function to the corresponding element. \newline % Row Count 8 (+ 1) `lapply(x, mean)`\{\{noshy\}\} \newline % Row Count 9 (+ 1) \{\{fa-bolt\}\} `rapply()` is a recursive version of `lapply()` with flexibility in\{\{noshy\}\} \newline % Row Count 11 (+ 2) how the result is structured. \newline % Row Count 12 (+ 1) `\textgreater{} rapply(x, sqrt, how = "list")`\{\{noshy\}\} \newline % Row Count 13 (+ 1) `\textgreater{} rapply(x, sqrt, how = "unlist")`\{\{noshy\}\} \newline % Row Count 14 (+ 1) \{\{fa-bolt\}\} `sapply()` is a user-friendly version and wrapper of `lapply()` by\{\{noshy\}\} \newline % Row Count 16 (+ 2) default returning a vector. \newline % Row Count 17 (+ 1) `\textgreater{} sapply(1:5,sqrt)`\{\{noshy\}\} \newline % Row Count 18 (+ 1) \{\{fa-bolt\}\} `vapply` is similar to `sapply`, but has a pre-specified type of\{\{noshy\}\} \newline % Row Count 20 (+ 2) return value, so it can be safer (and sometimes faster) to use. \newline % Row Count 22 (+ 2) `\textgreater{} vapply(1:5, sqrt, numeric(1))`\{\{noshy\}\} \newline % Row Count 23 (+ 1) \{\{fa-bolt\}\} `mapply()` is a multivariate version of` sapply()` that applies a\{\{noshy\}\} \newline % Row Count 25 (+ 2) function to all first (then second, third, and so on) elements of \newline % Row Count 27 (+ 2) its arguments. Arguments are recycled if necessary. \newline % Row Count 29 (+ 2) `\textgreater{} mapply(rep, 1:2, 2:1)`\{\{noshy\}\} \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{The apply family (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{fa-bolt\}\} `tapply()` applies a function to each (non-empty) group of\{\{noshy\}\} \newline % Row Count 2 (+ 2) values given by a unique combination of the levels of certain \newline % Row Count 4 (+ 2) factors. \newline % Row Count 5 (+ 1) `\textgreater{} with(dat, tapply(age, gender, mean))`\{\{noshy\}\} \newline % Row Count 6 (+ 1) This could have also written as: \newline % Row Count 7 (+ 1) `\textgreater{} dat \%\$\% tapply(age, gender, mean)`\{\{noshy\}\} \newline % Row Count 8 (+ 1) `\%\$\%` exposes the contents of the left-hand side object to the expression on the right.\{\{noshy\}\}% Row Count 10 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(**Basics)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Time Performance of the different approaches}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/worlddoit_1673637450_ экрана 2023-01-13 в 20.16.24.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Friends of map}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\{\{fa-bolt\}\} The `modify` friend of `map`\{\{noshy\}\} \newline % Row Count 1 (+ 1) The `purrr::modify()` function tackles transformations that preserve the type input in the output.\{\{noshy\}\} \newline % Row Count 4 (+ 3) `\textgreater{} modify(df, ∼ .x * 2)`\{\{noshy\}\} \newline % Row Count 5 (+ 1) Things are not modified in place but new objects are created \newline % Row Count 7 (+ 2) \{\{fa-bolt\}\} The `walk` friend of `map`\{\{noshy\}\} \newline % Row Count 8 (+ 1) The `purrr::walk()` function tackles transformations that do not require to store the output but are \{\{noshy\}\}focused only on side-effects \newline % Row Count 11 (+ 3) \{\{fa-bolt\}\} The `imap` friend of `map`\{\{noshy\}\} \newline % Row Count 12 (+ 1) The `purrr::imap()` function and friends essentially mimic ways of looping:\{\{noshy\}\} \newline % Row Count 14 (+ 2) `\textgreater{} imap(x, ∼ paste0("Label: ", .y, " Value: ", .x))`\{\{noshy\}\} \newline % Row Count 16 (+ 2) `\textgreater{} imap\_chr(x, ∼ paste0("Label: ", .y, " Value: ", .x))`\{\{noshy\}\}% Row Count 18 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Predicate functionals}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Predicate functions}} return `TRUE` or `FALSE`. For instance, the testing functions is `.x()`. \{\{noshy\}\}Predicate functionals apply a predicate to the elements of a vector \newline % Row Count 4 (+ 4) `\textgreater{} x\textless{}-list(1:2, c("a","b"), c(TRUE, FALSE))`\{\{noshy\}\} \newline % Row Count 6 (+ 2) `\textgreater{} some(x, is.logical)`\{\{noshy\}\} \newline % Row Count 7 (+ 1) `{[}1{]} TRUE`\{\{noshy\}\} \newline % Row Count 8 (+ 1) `\textgreater{} every(x, is.vector)`\{\{noshy\}\} \newline % Row Count 9 (+ 1) `{[}1{]} TRUE`\{\{noshy\}\} \newline % Row Count 10 (+ 1) `\textgreater{} detect(x, is.logical); detect\_index(x, is.logical)`\{\{noshy\}\} \newline % Row Count 12 (+ 2) `{[}1{]} TRUE FALSE`\{\{noshy\}\} \newline % Row Count 13 (+ 1) `{[}1{]} 3`\{\{noshy\}\} \newline % Row Count 14 (+ 1) `\textgreater{} keep(x, is.character)`\{\{noshy\}\} \newline % Row Count 15 (+ 1) `{[}{[}1{]}{]}`\{\{noshy\}\} \newline % Row Count 16 (+ 1) `{[}1{]} "a" "b"`\{\{noshy\}\} \newline % Row Count 17 (+ 1) `\textgreater{} discard(x, is.integer)`\{\{noshy\}\} \newline % Row Count 18 (+ 1) `{[}{[}1{]}{]} {[}{[}2{]}{]}`\{\{noshy\}\} \newline % Row Count 19 (+ 1) `{[}1{]} "a" "b" {[}1{]} TRUE FALSE`\{\{noshy\}\}% Row Count 20 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(**Basics)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Function Factories}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Functions that make functions. \newline % Row Count 1 (+ 1) `\textgreater{} log10`\{\{noshy\}\} \newline % Row Count 2 (+ 1) `function (x) .Primitive("log10")`\{\{noshy\}\} \newline % Row Count 3 (+ 1) `\textgreater{} changelog \textless{}- function(b) \{`\{\{noshy\}\} \newline % Row Count 4 (+ 1) `+ function(x) \{`\{\{noshy\}\} \newline % Row Count 5 (+ 1) `+ log(x)/log(b) \}\}`\{\{noshy\}\} \newline % Row Count 6 (+ 1) ` log10 \textless{}- changelog(10)`\{\{noshy\}\} \newline % Row Count 7 (+ 1) `\textgreater{} log10(10)`\{\{noshy\}\} \newline % Row Count 8 (+ 1) `{[}1{]} 1`\{\{noshy\}\} \newline % Row Count 9 (+ 1) `\textgreater{} log10`\{\{noshy\}\} \newline % Row Count 10 (+ 1) `function(x) \{`\{\{noshy\}\} \newline % Row Count 11 (+ 1) `log(x)/log(b)\}`\{\{noshy\}\} \newline % Row Count 12 (+ 1) `\textless{}environment: 0x000000000e46a9e8\textgreater{} \#notice the environment`\{\{noshy\}\} \newline % Row Count 14 (+ 2) The enclosing environment of the ` log10` is the execution environments of the `changelog` function \{\{noshy\}\} when ` log10` was defined by assignment.\{\{noshy\}\} \newline % Row Count 18 (+ 4) There is however a 'bug' due to lazy evaluation so it is better to always force the function parameter. \newline % Row Count 21 (+ 3) `\textgreater{} changelog \textless{}- function(b) \{`\{\{noshy\}\} \newline % Row Count 22 (+ 1) `+ force(b)`\{\{noshy\}\} \newline % Row Count 23 (+ 1) `+ function(x) \{log(x)/log(b)\}`\{\{noshy\}\} \newline % Row Count 24 (+ 1) `+ \}`\{\{noshy\}\} \newline % Row Count 25 (+ 1) You can combine factories with functionals \newline % Row Count 26 (+ 1) `\textgreater{} names \textless{}- list(log2 = 2,`\{\{noshy\}\} \newline % Row Count 27 (+ 1) `+ log3 = 3,`\{\{noshy\}\} \newline % Row Count 28 (+ 1) `+ log10 = 10)`\{\{noshy\}\} \newline % Row Count 29 (+ 1) `\textgreater{} (logs \textless{}- purrr::map(names, changelog))`\{\{noshy\}\} \newline % Row Count 31 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Function Factories (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`\$log2`\{\{noshy\}\} \newline % Row Count 1 (+ 1) `function(x) \{log(x)/log(b)\}`\{\{noshy\}\} \newline % Row Count 2 (+ 1) `\textless{}bytecode: 0x000000000dbc6b98\textgreater{}`\{\{noshy\}\} \newline % Row Count 3 (+ 1) `\textless{}environment: 0x000000000e03dc20\textgreater{}`\{\{noshy\}\} \newline % Row Count 4 (+ 1) `\textgreater{} logs\$log2(2)`\{\{noshy\}\} \newline % Row Count 5 (+ 1) `{[}1{]} 1`\{\{noshy\}\} \newline % Row Count 6 (+ 1) `\textgreater{} logs\$log10(10)`\{\{noshy\}\} \newline % Row Count 7 (+ 1) `{[}1{]} 1`\{\{noshy\}\} \newline % Row Count 8 (+ 1) `\textgreater{} logs\$log3(3)`\{\{noshy\}\} \newline % Row Count 9 (+ 1) `{[}1{]} 1`\{\{noshy\}\} \newline % Row Count 10 (+ 1) You can use factories to pass different arguments according to your needs to other functions. \newline % Row Count 12 (+ 2) `\textgreater{} n \textless{}- 100; sd \textless{}- c(1, 5, 15)`\{\{noshy\}\} \newline % Row Count 13 (+ 1) `\textgreater{} df \textless{}- data.frame(x = rnorm(3*n, sd = sd), sd = rep(sd, n))`\{\{noshy\}\} \newline % Row Count 15 (+ 2) `\textgreater{} histograms\textless{}-ggplot(df, aes(x)) +`\{\{noshy\}\} \newline % Row Count 16 (+ 1) `+ geom\_histogram(binwidth = 2) +`\{\{noshy\}\} \newline % Row Count 17 (+ 1) `+ facet\_wrap(∼ sd, scales = "free\_x") +`\{\{noshy\}\} \newline % Row Count 19 (+ 2) `+ labs(x = NULL)`\{\{noshy\}\} \newline % Row Count 20 (+ 1) `\textgreater{} jpeg("histograms.jpeg")`\{\{noshy\}\} \newline % Row Count 21 (+ 1) `\textgreater{} plot(histograms)`\{\{noshy\}\} \newline % Row Count 22 (+ 1) `\textgreater{} dev.off()`\{\{noshy\}\} \newline % Row Count 23 (+ 1) `null device`\{\{noshy\}\} \newline % Row Count 24 (+ 1) The code above creates `binwidth` facets in which the is the same, this is not ideal to compare the \{\{noshy\}\}different histograms. \newline % Row Count 27 (+ 3) {\bf{Output 1}} \newline % Row Count 28 (+ 1) we can create a variable bin width \newline % Row Count 29 (+ 1) `\textgreater{} binwidth\_bins \textless{}- function(n) \{`\{\{noshy\}\} \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Function Factories (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`+ force(n)`\{\{noshy\}\} \newline % Row Count 1 (+ 1) `+ function(x) \{`\{\{noshy\}\} \newline % Row Count 2 (+ 1) `+ (max(x) - min(x)) / n`\{\{noshy\}\} \newline % Row Count 3 (+ 1) `+ \}`\{\{noshy\}\} \newline % Row Count 4 (+ 1) `+ \}`\{\{noshy\}\} \newline % Row Count 5 (+ 1) and then we run a new groups of histograms \newline % Row Count 6 (+ 1) `\textgreater{} histograms2\textless{}-ggplot(df, aes(x)) +`\{\{noshy\}\} \newline % Row Count 7 (+ 1) `+ geom\_histogram(binwidth = binwidth\_bins(20)) +`\{\{noshy\}\} \newline % Row Count 9 (+ 2) `+ facet\_wrap(∼ sd, scales = "free\_x") +`\{\{noshy\}\} \newline % Row Count 11 (+ 2) `+ labs(x = NULL)`\{\{noshy\}\} \newline % Row Count 12 (+ 1) `\textgreater{} jpeg("histograms2.jpeg")`\{\{noshy\}\} \newline % Row Count 13 (+ 1) `\textgreater{} plot(histograms2)`\{\{noshy\}\} \newline % Row Count 14 (+ 1) `\textgreater{} dev.off()`\{\{noshy\}\} \newline % Row Count 15 (+ 1) `null device`\{\{noshy\}\} \newline % Row Count 16 (+ 1) Now the `binwidth` varies and keeps constant the number of observations in each bin\{\{noshy\}\} \newline % Row Count 18 (+ 2) {\bf{Output 2}}% Row Count 19 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(**Basics)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Output 1}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/worlddoit_1674264626_ экрана 2023-01-21 в 2.22.35.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Output 2}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/worlddoit_1674264674_ экрана 2023-01-21 в 2.28.29.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Function Operators}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Functions that take functions as arguments and return other functions. They wrap a function and somehow extend its behavior without modifying the function output (decorator). \newline % Row Count 4 (+ 4) `\textgreater{} log(10)`\{\{noshy\}\} \newline % Row Count 5 (+ 1) `{[}1{]} 2.302585`\{\{noshy\}\} \newline % Row Count 6 (+ 1) `\textgreater{} log("a")`\{\{noshy\}\} \newline % Row Count 7 (+ 1) `Error in log("a") : non-numeric argument to mathematical function`\{\{noshy\}\} \newline % Row Count 9 (+ 2) `\textgreater{} safe\_log \textless{}- purrr::safely(log)`\{\{noshy\}\} \newline % Row Count 10 (+ 1) `\textgreater{} safe\_log`\{\{noshy\}\} \newline % Row Count 11 (+ 1) `function (...)`\{\{noshy\}\} \newline % Row Count 12 (+ 1) `capture\_error(.f(...), otherwise, quiet)`\{\{noshy\}\} \newline % Row Count 14 (+ 2) `\textless{}bytecode: 0x000000001b790a88\textgreater{}`\{\{noshy\}\} \newline % Row Count 15 (+ 1) `\textless{}environment: 0x000000000ed506f0\textgreater{}`\{\{noshy\}\} \newline % Row Count 16 (+ 1) `\textgreater{} safe\_log(10)`\{\{noshy\}\} \newline % Row Count 17 (+ 1) `\$result`\{\{noshy\}\} \newline % Row Count 18 (+ 1) `{[}1{]} 2.302585`\{\{noshy\}\} \newline % Row Count 19 (+ 1) `\$error`\{\{noshy\}\} \newline % Row Count 20 (+ 1) `NULL`\{\{noshy\}\} \newline % Row Count 21 (+ 1) `\textgreater{} safe\_log("a")`\{\{noshy\}\} \newline % Row Count 22 (+ 1) `\$result`\{\{noshy\}\} \newline % Row Count 23 (+ 1) `NULL`\{\{noshy\}\} \newline % Row Count 24 (+ 1) `\$error`\{\{noshy\}\} \newline % Row Count 25 (+ 1) `\textless{}simpleError in .Primitive("log")(x, base):`\{\{noshy\}\} \newline % Row Count 27 (+ 2) `non-numeric argument to mathematical function\textgreater{}`\{\{noshy\}\} \newline % Row Count 29 (+ 2) `safely()` is also useful in catching errors in the applications of functionals like `map`.\{\{noshy\}\} \newline % Row Count 32 (+ 3) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Function Operators (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`\textgreater{} out \textless{}- map(x, safely(sum))`\{\{noshy\}\} \newline % Row Count 1 (+ 1) `\textgreater{} str(out)`\{\{noshy\}\} \newline % Row Count 2 (+ 1) `List of 4`\{\{noshy\}\} \newline % Row Count 3 (+ 1) `\$ :List of 2`\{\{noshy\}\} \newline % Row Count 4 (+ 1) `..\$ result: int 10`\{\{noshy\}\} \newline % Row Count 5 (+ 1) `..\$ error : NULL`\{\{noshy\}\} \newline % Row Count 6 (+ 1) `...`\{\{noshy\}\} \newline % Row Count 7 (+ 1) `\$ :List of 2`\{\{noshy\}\} \newline % Row Count 8 (+ 1) `..\$ result: NULL`\{\{noshy\}\} \newline % Row Count 9 (+ 1) `..\$ error :List of 2`\{\{noshy\}\} \newline % Row Count 10 (+ 1) `.. ..\$ message: chr "invalid 'type' (character) of argument"`\{\{noshy\}\} \newline % Row Count 12 (+ 2) `.. ..\$ call : language .Primitive("sum")(..., na.rm = na.rm)`\{\{noshy\}\} \newline % Row Count 14 (+ 2) `.. ..- attr(*, "class")= chr {[}1:3{]} "simpleError" "error" "condition"`\{\{noshy\}\} \newline % Row Count 16 (+ 2) `...`\{\{noshy\}\}% Row Count 17 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Summary}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/worlddoit_1674265967_ экрана 2023-01-20 в 11.21.25.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{resources: \{\{popup="https://adv-r.hadley.nz/fp.html"\}\}1\{\{/popup\}\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}