\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{josi68} \pdfinfo{ /Title (introduction-to-r.pdf) /Creator (Cheatography) /Author (josi68) /Subject (Introduction to 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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{Introduction to R Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{josi68} via \textcolor{DarkBackground}{\uline{cheatography.com/197242/cs/41550/}}} \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}josi68 \\ \uline{cheatography.com/josi68} \\ \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 4th December, 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{4.32999 cm} p{0.64701 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Arithmetics}} \tn % Row 0 \SetRowColor{LightBackground} Addition & + \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Subtraction & - \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Multiplication & * \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} Division & / \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} Modulo & \%\% \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} Exponentiation & \textasciicircum{} \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\emph{Modulo returns the remainder of the division of the number to the left by the number on its right, for example 5 modulo 3 or 5 \%\% 3 is 2.}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{4.4793 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Comparison operators}} \tn % Row 0 \SetRowColor{LightBackground} Less than & \textless{} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} More than & \textgreater{} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Less than or equal to & \textless{}= \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} Greater than or equal to & \textgreater{}= \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} Equal to each other & == \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} Not equal to each other & != \tn % Row Count 6 (+ 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}{Selecting by comparison}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Poker and roulette winnings from Monday to Friday: \newline poker\_vector \textless{}- c(140, -50, 20, -120, 240) \newline roulette\_vector \textless{}- c(-24, -50, 100, -350, 10) \newline days\_vector \textless{}- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") \newline names(poker\_vector) \textless{}- days\_vector \newline names(roulette\_vector) \textless{}- days\_vector \newline \newline \# Which days did you make money on roulette? \newline selection\_vector \textless{}- roulette\_vector \textgreater{} 0 \newline \newline \# Select from roulette\_vector these days \newline roulette\_winning\_days \textless{}- roulette\_vector{[}selection\_vector{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.78503 cm} x{1.51041 cm} x{1.28156 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Data Types}} \tn % Row 0 \SetRowColor{LightBackground} Decimal values & 4.5 & Numerics \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Whole numbers & 4 & Integers \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Boolean values & TRUE / FALSE & Logical \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} Text / String & "Text" & Characters \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{Show the data type: class({\emph{data}})} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.04057 cm} x{2.93643 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Lists}} \tn % Row 0 \SetRowColor{LightBackground} Create a list & my\_list \textless{}- list(element1, element2) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Give names to the list items & my\_list \textless{}- list(name1 = your\_comp1, name2 = your\_comp2) \tn % Row Count 6 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\# Adapt list() call to give the components names \newline my\_list \textless{}- list(vec = my\_vector, \newline mat = my\_matrix, \newline df = my\_df) \newline \newline \#or if the list was alread created \newline names(my\_list) \textless{}- c("vec", "mat", "df")} \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}{Selecting components in a list}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{One way to select a component is using the numbered position of that component. For example, to "grab" the first component of shining\_list you type \newline % Row Count 3 (+ 3) shining\_list{[}{[}1{]}{]} \newline % Row Count 4 (+ 1) A quick way to check this out is typing it in the console. Important to remember: to select elements from vectors, you use single square brackets: {[} {]}. Don't mix them up! \newline % Row Count 8 (+ 4) You can also refer to the names of the components, with {[}{[} {]}{]} or with the \$ sign. Both will select the data frame representing the reviews: \newline % Row Count 11 (+ 3) shining\_list{[}{[}"reviews"{]}{]} \newline % Row Count 12 (+ 1) shining\_list\$reviews \newline % Row Count 13 (+ 1) Besides selecting components, you often need to select specific elements out of these components. For example, with shining\_list{[}{[}2{]}{]}{[}1{]} you select from the second component, actors (shining\_list{[}{[}2{]}{]}), the first element ({[}1{]}). When you type this in the console, you will see the answer is Jack Nicholson.% Row Count 20 (+ 7) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Vector Basics}} \tn % Row 0 \SetRowColor{LightBackground} Assign value to variable & my\_var \textless{}- 4 \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Numeric vector & numeric\_vector \textless{}- c(1, 10, 49) \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Character\_vector & character\_vector \textless{}- c("a", "b", "c") \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} Boolean vector & boolean\_vector \textless{}- c(TRUE, FALSE, TRUE) \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} Naming a vector & \seqsplit{names(numeric\_vector)} \textless{}- c("Jack", "Jill", "Johanna") \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} Sum of the elements in the vector & sum(vector\_name) \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} Select element 3 of the vector & element \textless{}- vector\_name{[}3{]} \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} Select elements 2, 3, 4, 5 of the vector & elements \textless{}- vector\_name{[}3:5{]} \tn % Row Count 17 (+ 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}{Factors}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Animals - Turn vector character elements into nominal factors \newline animals\_vector \textless{}- c("Elephant", "Giraffe", "Donkey", "Horse") \newline factor\_animals\_vector \textless{}- factor(animals\_vector) \newline factor\_animals\_vector \newline \newline \# Temperature - Turn vector character elements into ordinal factors \newline temperature\_vector \textless{}- c("High", "Low", "High","Low", "Medium") \newline factor\_temperature\_vector \textless{}- \seqsplit{factor(temperature\_vector}, order = TRUE, levels = c("Low", "Medium", "High")) \newline factor\_temperature\_vector} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{When factors are ordinal:: \newline order = TRUE \newline \newline To give the order of the ordinal factors: \newline levels = c("Low", "Medium", "High"))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{3.38436 cm} x{1.59264 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Data Frames}} \tn % Row 0 \SetRowColor{LightBackground} Show the first couple of lines & \seqsplit{head(data\_frame)} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Show the last couple of lines & \seqsplit{tail(data:frame)} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Summarize data frame (min, max, median, quartiles) & \seqsplit{summary(data\_frame)} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} Structure (nr. obs, var., names, data type...) & \seqsplit{str(data\_frame)} \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{unlike matrixes, df can have different types of data - BUT all variables need to have the same length (unlike for lists)}}} \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}{Create data frame from vectors + select values}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Definition of vectors \newline name \textless{}- c("Mercury", "Venus", "Earth", \newline "Mars", "Jupiter", "Saturn", \newline "Uranus", "Neptune") \newline type \textless{}- c("Terrestrial planet", \newline "Terrestrial planet", \newline "Terrestrial planet", \newline "Terrestrial planet", "Gas giant", \newline "Gas giant", "Gas giant", "Gas giant") \newline diameter \textless{}- c(0.382, 0.949, 1, 0.532, \newline 11.209, 9.449, 4.007, 3.883) \newline rotation \textless{}- c(58.64, -243.02, 1, 1.03, \newline 0.41, 0.43, -0.72, 0.67) \newline rings \textless{}- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE) \newline \newline \# Create a data frame from the vectors \newline planets\_df \textless{}- data.frame(name, type, diameter, rotation, rings) \newline \newline \# Select first 5 values of diameter column \newline planets\_df{[}1:5, "diameter"{]} \newline \newline \# Select the rings variable from planets\_df \newline rings\_vector \textless{}- planets\_df\$rings \newline \newline \# Select planets with diameter \textless{} 1 \newline subset(planets\_df, subset = diameter \textless{}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}{Order the data}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{In data analysis you can sort your data according to a certain variable in the dataset. In R, this is done with the help of the function order(). \newline % Row Count 3 (+ 3) order() is a function that gives you the ranked position of each element when it is applied on a variable, such as a vector for example: \newline % Row Count 6 (+ 3) a \textless{}- c(100, 10, 1000) \newline % Row Count 7 (+ 1) order(a) \newline % Row Count 8 (+ 1) {[}1{]} 2 1 3 \newline % Row Count 9 (+ 1) 10, which is the second element in a, is the smallest element, so 2 comes first in the output of order(a). 100, which is the first element in a is the second smallest element, so 1 comes second in the output of order(a). \newline % Row Count 14 (+ 5) This means we can use the output of order(a) to reshuffle a: \newline % Row Count 16 (+ 2) a{[}order(a){]} \newline % Row Count 17 (+ 1) {[}1{]} 10 100 1000% Row Count 18 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Matrices}} \tn % Row 0 \SetRowColor{LightBackground} Construct Matrix with 3 rows that contain the numbers 1 to 9 & matrix(1:9, byrow = TRUE, nrow = 3) \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} From Vector to Matrix & Matrix\_names \textless{}- matrix(vector\_name, byrow = TRUE, nrow = 3) \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} Totals for each row of a matrix & rowSums(my\_matrix) \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} Total for each row of a matrix & colSums() \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} Adding columns & big matrix \textless{}- cbind(vector1, matrix1) \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} Adding rows & rbind \tn % Row Count 13 (+ 1) % Row 6 \SetRowColor{LightBackground} Select all elements of the first column & matrix{[},1{]} \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} Select all elements of the first row & matrix{[}1,{]} \tn % Row Count 17 (+ 2) % Row 8 \SetRowColor{LightBackground} Select 2nd element of 3rd column & matrix{[}2,3{]} \tn % Row Count 19 (+ 2) % Row 9 \SetRowColor{white} Create matrix with the data on the rows 1, 2, 3 and columns 2, 3, 4. & matrix{[}1:3,2:4{]} \tn % Row Count 23 (+ 4) % Row 10 \SetRowColor{LightBackground} Average of the matrix elements & mean(matrix\_name) \tn % Row Count 25 (+ 2) % Row 11 \SetRowColor{white} Summary of Matrix (and other stuff) & \seqsplit{summary(matrix\_name)} \tn % Row Count 27 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\emph{The argument byrow indicates that the matrix is filled by the rows. If we want the matrix to be filled by the columns, we just place byrow = FALSE}} \newline \newline {\bf{all data in a matrix should be of the same type. Otherwise, create a data frame}}} \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}{Naming a Matrix}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Box office Star Wars (in millions!) \newline new\_hope \textless{}- c(460.998, 314.4) \newline empire\_strikes \textless{}- c(290.475, 247.900) \newline return\_jedi \textless{}- c(309.306, 165.8) \newline \newline \# Construct matrix \newline star\_wars\_matrix \textless{}- matrix(c(new\_hope, empire\_strikes, return\_jedi), nrow = 3, byrow = TRUE) \newline \newline \# Vectors region and titles, used for naming \newline region \textless{}- c("US", "non-US") \newline titles \textless{}- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi") \newline \newline \# Name the columns with region \newline colnames(star\_wars\_matrix) \textless{}- region \newline \newline \# Name the rows with titles \newline rownames(star\_wars\_matrix) \textless{}- titles \newline \newline \# Print out star\_wars\_matrix \newline star\_wars\_matrix} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}