\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{Todin} \pdfinfo{ /Title (base-r.pdf) /Creator (Cheatography) /Author (Todin) /Subject (Base 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}{050505} \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{Base R Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Todin} via \textcolor{DarkBackground}{\uline{cheatography.com/174736/cs/36706/}}} \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}Todin \\ \uline{cheatography.com/todin} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 25th January, 2023.\\ Updated 25th 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*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Language itself}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{R is a free software environment for statistical computing and graphics. \newline % Row Count 2 (+ 2) It's a compiled language. \newline % Row Count 3 (+ 1) R is a dynamically-typed,that means the type of a variable is determined at runtime, and that variables do not have to be explicitly declared with a specific type. \newline % Row Count 7 (+ 4) R is weakly-typed, which means that it will automatically convert between types when necessary, without the need for explicit type casting. \newline % Row Count 10 (+ 3) R is a garbage-collected language. This means the R runtime system automatically manages the memory used by the program. \newline % Row Count 13 (+ 3) R is both a functional an object oriented language but it doesn't support inheritance, polymorphism, and encapsulation. \newline % Row Count 16 (+ 3) In R, functions call by value. This means that when a function is called, the values of the arguments passed to the function are passed to the function's local scope, and any changes made to the arguments within the function do not affect the original values of the arguments outside the function.% Row Count 22 (+ 6) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Language environment}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{To code in R you can install the extension in your IDE like VSC or install its own IDE {\emph{R Studio}} : \newline % Row Count 2 (+ 2) \{\{link="https://cran.r-project.org/bin/windows/base/"\}\}R for Windows\{\{/link\}\} \newline % Row Count 4 (+ 2) \{\{link="https://cran.r-project.org/bin/linux/ubuntu/fullREADME.html"\}\}R for Linux\{\{/link\}\} \newline % Row Count 6 (+ 2) Then to run it you can run it with the IDE if you don't have one you can use R Studio server, which is a web-based version of R Studio that can be accessed through a web browser. The command for R Studio is `Rscript myscript.R` . \newline % Row Count 11 (+ 5) When installed R Studio it would have a minimal impact impact on the local machine but if you runs large R scripts or using R for data analysis and machine learning tasks, which require a lot of memory,it may have a bigger impact on the local machine. But in the end it does not modify something in your machine that would block you in it's use.% Row Count 18 (+ 7) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Library}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Code}} \newline % Row Count 1 (+ 1) \seqsplit{`install.packages('dplyr')`} : Download and install a package from CRAN. \newline % Row Count 3 (+ 2) `library(dplyr')` : Load the package into the file. \newline % Row Count 5 (+ 2) `dplyr::select` : Use a function from the package \newline % Row Count 6 (+ 1) {\bf{Commonly used library}} \newline % Row Count 7 (+ 1) {\emph{stringr}} for string manipulation \newline % Row Count 8 (+ 1) {\emph{dplyr}} for data frame manipulation \newline % Row Count 9 (+ 1) {\emph{ggplot2}} for plotting graphs \newline % Row Count 10 (+ 1) {\emph{lubridate}} for date% Row Count 11 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{To go further}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{R is an integrated suite of software facilities for data manipulation, calculation and graphical display. It includes: \newline % Row Count 3 (+ 3) -An effective data handling and storage facility, \newline % Row Count 4 (+ 1) -A suite of operators for calculations on arrays, in particular matrices, \newline % Row Count 6 (+ 2) -A large, coherent, integrated collection of intermediate tools for data analysis, \newline % Row Count 8 (+ 2) -Graphical facilities for data analysis and display either on-screen or on hardcopy, and \newline % Row Count 10 (+ 2) -A well-developed, simple and effective programming language which includes conditionals, loops, user-defined recursive functions and input and output facilities. \newline % Row Count 14 (+ 4) \{\{link="https://careerkarma.com/blog/who-uses-r/"\}\}The people who mainly use R are data scientist / engineer\{\{/link\}\}% Row Count 17 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Syntax and data structures}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Variable Assignment}} \newline % Row Count 1 (+ 1) `a \textless{}- 'bonjour' ` : {\emph{string}} \newline % Row Count 2 (+ 1) `a \textless{}- 6` : {\emph{integer}} \newline % Row Count 3 (+ 1) {\bf{Loop}} \newline % Row Count 4 (+ 1) For loop : \newline % Row Count 5 (+ 1) `for (variable in sequence)\{ Do something \}` \newline % Row Count 6 (+ 1) While loop : \newline % Row Count 7 (+ 1) `while (condition)\{ Do something \}` \newline % Row Count 8 (+ 1) {\bf{Condition}} \newline % Row Count 9 (+ 1) `if (condition)\{ Do something \} else \{ Do something different \}` \newline % Row Count 11 (+ 2) `a == b` : Are equal \newline % Row Count 12 (+ 1) `a \textgreater{} b` : Greater than \newline % Row Count 13 (+ 1) `a \textgreater{}= b` : Greater than or equal to \newline % Row Count 14 (+ 1) `is.na(a)` : Is missing \newline % Row Count 15 (+ 1) `a != b` : Not equal \newline % Row Count 16 (+ 1) `a \textless{} b` : Less than \newline % Row Count 17 (+ 1) `a \textless{}= b` : Less than or equal to \newline % Row Count 18 (+ 1) `is.null(a)` : Is null \newline % Row Count 19 (+ 1) {\bf{Functions}} \newline % Row Count 20 (+ 1) ` function\_name \textless{}- function (var)\{ Do something return (new\_variable) \}` \newline % Row Count 22 (+ 2) {\bf{Maths Functions}} \newline % Row Count 23 (+ 1) `log(x) `: Natural log / `sum(x)` : Sum \newline % Row Count 24 (+ 1) `exp(x) `: Exponential / `mean(x) `: Mean \newline % Row Count 25 (+ 1) `max(x)` : Largest element / `median(x)` : Median \newline % Row Count 26 (+ 1) `min(x)` : Smallest element /` quantile(x)` : Percentage quantiles \newline % Row Count 28 (+ 2) `round(x, n)` : Round to n decimal places \newline % Row Count 29 (+ 1) `rank(x)` : Rank of elements \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Syntax and data structures (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`signif(x, n)` : Round to n significant figures \newline % Row Count 1 (+ 1) `var(x)` : The variance / `cor(x, y)` : Correlation \newline % Row Count 3 (+ 2) `sd(x)` : The standard deviation \newline % Row Count 4 (+ 1) {\bf{Lists}} \newline % Row Count 5 (+ 1) `l \textless{}- list(x = 1:5, y = c('a', 'b'))` : A list is collection of elements which can be of diļ¬€erent types. \newline % Row Count 8 (+ 3) `l{[}{[}2{]}{]}` : second element of l \newline % Row Count 9 (+ 1) `l{[}1{]}` : new list with only the first element \newline % Row Count 10 (+ 1) `l\$x` : element named x \newline % Row Count 11 (+ 1) `l{[}'y'{]}` : new list with only element named y \newline % Row Count 12 (+ 1) {\bf{String}} \newline % Row Count 13 (+ 1) `paste(x, y, sep = ' ')` : Join multiple vectors together. \newline % Row Count 15 (+ 2) `paste(x, collapse = ' ')` : Join elements of a vector together. \newline % Row Count 17 (+ 2) `grep(pattern, x)` : Find regular expression matches in x. \newline % Row Count 19 (+ 2) `gsub(pattern, replace, x)` : Replace matches in x with a string. \newline % Row Count 21 (+ 2) `toupper(x)` : Convert to uppercase. \newline % Row Count 22 (+ 1) `tolower(x)` : Convert to lowercase. \newline % Row Count 23 (+ 1) `nchar(x)` : Number of characters in a string. \newline % Row Count 24 (+ 1) {\bf{Matrixes}} \newline % Row Count 25 (+ 1) `m \textless{}- matrix(x, nrow = 3, ncol = 3)` : Create a matrix from x. \newline % Row Count 27 (+ 2) `df{[} , 2{]}` : Select a column . \newline % Row Count 28 (+ 1) `df{[}2, {]}` : Select a row. \newline % Row Count 29 (+ 1) `df{[}2, 2{]}` : Select an element. \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Syntax and data structures (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Data Frames}} \newline % Row Count 1 (+ 1) `df \textless{}- data.frame(x = 1:3, y = c('a', 'b', 'c'))` : A special case of a list where all elements are the same length. \newline % Row Count 4 (+ 3) `View(df)` : See the full data frame. \newline % Row Count 5 (+ 1) `head(df)` :See the first 6 rows. \newline % Row Count 6 (+ 1) `nrow(df)` : Number of rows. \newline % Row Count 7 (+ 1) `ncol(df)` : Number of columns. \newline % Row Count 8 (+ 1) `dim(df)` : Number of columns and rows. \newline % Row Count 9 (+ 1) As the same sub settings as matrixes and lists \newline % Row Count 10 (+ 1) {\bf{Ploting}} \newline % Row Count 11 (+ 1) `plot(x)` : plot values of x in order \newline % Row Count 12 (+ 1) `plot(x, y)` : plot values of x against y \newline % Row Count 13 (+ 1) {\bf{Reading and writing data}} \newline % Row Count 14 (+ 1) `df \textless{}- read.table('file.txt')` / `df \textless{}- read.csv('file.csv') ` / Read a file text or a csv \newline % Row Count 16 (+ 2) `write.table(df, 'file.txt')` / `write.csv(df, 'file.csv')` / Write a file text or a csv% Row Count 18 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}