\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{MacaSalva} \pdfinfo{ /Title (r-base.pdf) /Creator (Cheatography) /Author (MacaSalva) /Subject (R Base 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}{A33460} \definecolor{LightBackground}{HTML}{F9F2F5} \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 Base Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{MacaSalva} via \textcolor{DarkBackground}{\uline{cheatography.com/93894/cs/20700/}}} \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}MacaSalva \\ \uline{cheatography.com/macasalva} \\ \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 8th October, 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Shortcuts}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`cmd + enter ` Runs the line you are in and goes to the next line} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`alt + enter ` Runs the line you are in} \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}{{\bf{Getting help}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`?` (Looks in your library)} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`??` (Looks in every library)} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`help.search("\_")` looks for a word or phrase} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`help(package="\_")` find help for a package} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Getting help is very useful when you are defining functions or other things, it provides information about all the different conditions possible.} \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}{{\bf{Working directory}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`getwd()` Tells you your working directory} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{`setwd('C://file/path')`How} to code your desired wd.} \tn % Row Count 3 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\emph{You can select it manually in RStudio by clicking in the gear.}} \newline \newline It has to be set to open correctly a .csv, if the file you desire to open is not in the same folder where you are saving your code you are going to need to define the path for opening without any problems.} \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}{{\bf{Working with .csv}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{To open a .csv:}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\emph{Whenever you open a csv, you have to save them with some name in order to be able to access to it and have it in your environment}}} \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`read.csv({\emph{path}}/{\emph{name of the csv}}, ... )` When opening a csv, look at the help of the function in order to see all the needs for each case.} \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{You can open manually a csv by clicking the import button in the environment. For this the `{\bf{readr}}` package is needed.} \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Result: `ds\textless{}-read.csv()` its always good to open the imported dataset to see if everything is correct. `View()`} \tn % Row Count 13 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{ To save a .csv:}}} \tn % Row Count 14 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`write.csv(df, "\_\_",...)`When saving a .csv look into the help to see all the possible variables that can be rearrange.} \tn % Row Count 17 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.14011 cm} x{2.83689 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Put things together}} \tn % Row 0 \SetRowColor{LightBackground} `paste("","", sep=" ")` & Pastes two things together \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `paste0()` & Pastes things without separator \tn % Row Count 4 (+ 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}{Creating functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`myfunction\textless{}- function (x)\{\{\{nl\}\} code \{\{nl\}\} paste() \{\{nl\}\}\}`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\emph{ Difference between paste, print and return}} \newline \newline {\emph{What happens inside the function stays in the function}}} \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}{Dates}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`as.Date( \_\_, format = "\_\_")` \newline % Row Count 1 (+ 1) In the first blank you have to put your dates, in the format how R needs to read it. \newline % Row Count 3 (+ 2) {\emph{RStudio understands that dates can exist and it knows how they work. when a variable is a date is going to show in the form : \{\{nl\}\}\{\{ac\}\}{\bf{year-month-day}}\{\{nl\}\} we can change this when we transform it into a character. Also, and because R understands dates as said before, when you are defining it from a string you have to explain how to read it.}} \newline % Row Count 11 (+ 8) {\bf{To get current date and time:}} \newline % Row Count 12 (+ 1) `Sys.Date()`% Row Count 13 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\emph{ You can ask for a sequence of dates if you use `seq(as.Date(),as.Date(),by= " ")` and select if you want it to be daily, weekly, monthly etc by saying so in the by= "day"}}} \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}{Lists}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`list(\_ , \_ , \_ )`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{To create a colum} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`list{[}{[}1{]}{]}`} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{To access to the first element of the list} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`list{[}{[}1{]}{]}{[}1{]}`} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{To access to the first element of the first element of the list} \tn % Row Count 7 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{To combine lists `\textless{}-c(list1,list2)`} \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.73735 cm} x{2.23965 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Tables of proportions}} \tn % Row 0 \SetRowColor{LightBackground} `prop.table(t)` & It sums up to 1 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `prop.table(t, 2)` & Column-wise \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `prop.table(t, 1)` & Row-wise \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}{Working with NA}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\emph{When working with NA's a lot of different operations don't work, because of that you have to ask for different things to obtain results}}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`any.NA()`} \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{It returns TRUE if one of the elements is NA} \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.59501 cm} x{1.46464 cm} x{2.51735 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Types of data}}}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Logical} & `TRUE, FALSE` & Boolean Values (T/F). \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{Numeric} & `2, 5, 7` & Interger or floating point numbers. \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{Character} & `"hello", "bye"` & Character strings. \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{Factor} & `"male", "female"` & Character strings with different levels. \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} & \textasciicircum{}{\emph{different levels}} \textasciicircum{} & \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} & levels(\_) & For assessing the levels \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} & `NA` & Missing values \tn % Row Count 13 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{{\emph{ For changing for one type to other you have to use the function `as.\_()`and it will transform in the type you decide. You can also transform dates.}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.14011 cm} x{2.83689 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Conditions}}}} \tn % Row 0 \SetRowColor{LightBackground} `! ` & Not \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `a == b` & Are equal \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `a != b` & Are not equal \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} `a \textgreater{} b` & Great than \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} `a \textless{} b` & Less than \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} `a \textgreater{}= b` & Greater or equal \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} `a \textless{}= b` & Less or equal \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} `is.na(a)` & Is missing \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} `is.null(a)` & Is null \tn % Row Count 9 (+ 1) % Row 9 \SetRowColor{white} `\& ` & And \tn % Row Count 10 (+ 1) % Row 10 \SetRowColor{LightBackground} `| ` & Or \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}{Operations with characters}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`substring("\_\_", first= \#, last= \#)`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Returns the characters inside the string within those positions} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`nchar()`} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Counts the number of characters (including symbols and spaces)} \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.50347 cm} x{2.70043 cm} x{1.3731 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Reshaping DataFrames}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{`melt`} & `(df, id.vars="", variable.name="")` & Transforms to long \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{`dcast`} & `(df, id\textasciitilde{}measure)` & Transform to wide \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{{\emph{To ask for help look for reshape2}} \newline \newline Look session 5} \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}{Table}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`prop.table(t)`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Returns a table with the proportions of all} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`prop.table(t,2)`} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Proportions by colums} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`prop.table(t,1)`} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Proportions by rows} \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}{{\bf{To round numbers:}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`round (x, \#)`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{It will round the x you ask with the decimals you ask (\#)} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`ceiling(\#)`} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{It will round to the next number} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`floor(\#)`} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{It will round in the number you have} \tn % Row Count 7 (+ 1) \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}{Conditionals}} \tn % Row 0 \SetRowColor{LightBackground} `if (\textless{}condition\textgreater{}) \{ \{\{nl\}\} \textless{}code\textgreater{} \{\{nl\}\}\}` & Just one condition \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} `if (\textless{}condition\textgreater{}) \{ \{\{nl\}\} \textless{}code\textgreater{}\{\{nl\}\} \} else \{\{\{nl\}\} \textless{}code\textgreater{} \{\{nl\}\}\}` & One condition, if not the rest without condition \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} `if (\textless{}condition\textgreater{}) \{ \{\{nl\}\} \textless{}code\textgreater{} \{\{nl\}\}\} else if (\textless{}condition\textgreater{}) \{\{\{nl\}\} \textless{}code\textgreater{} \{\{nl\}\}\} else \{\{\{nl\}\} \textless{}code\textgreater{} \{\{nl\}\}\}` & More than one condition \tn % Row Count 13 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\emph{You have to be careful when you place your conditions, the one that conditions the most have to be the first and so on}}} \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}{Creating Data Frames}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`df\textless{}- \seqsplit{data.frame("name1"=c(values)},\{\{nl\}\} "name2"=c(values), etc)`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{You have to define the column names and give it the values that you want. The values can be a vector, a list or other things.} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{For getting things from data sets: `df{[}{[}\_{]}{]}` You can put the number of the row but is better putting the name.} \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Is very useful to use the command \$ to access a data frame `df\$columname`} \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{For adding things:} \tn % Row Count 11 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`rbind (df1,df2)`} \tn % Row Count 12 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\emph{Fails: row numbers don't match}}} \tn % Row Count 13 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`cbind ( df1,df2)`} \tn % Row Count 14 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\emph{ Fails: column names don't match}}} \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`aggregate()`} \tn % Row Count 16 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textasciicircum{}session 6\textasciicircum{} To do aggregations and get the result formatted as a data.frame} \tn % Row Count 18 (+ 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}{Apply}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`lapply( \_\_, function)`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{apply a function over a list or a vector} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`sapply(\_\_, function)`} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{same as lapply but with simplified results (better)} \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`tapply(\_\_, grouping,function)`} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Apply a function over a ragged array} \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\emph{Session 6}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.88666 cm} x{2.09034 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Math}} \tn % Row 0 \SetRowColor{LightBackground} `sqrt()` & Square root of the number \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `log()` & Logarithm of the number \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `abs()` & Absolute value \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} `+` & Sum \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} `-` & Substraction \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} `*` & Multiplication \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} `/` & Division \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} `\textasciicircum{}` & Exponential \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} `\%\%` & Module operator \tn % Row Count 11 (+ 1) % Row 9 \SetRowColor{white} `union()` & Union \tn % Row Count 12 (+ 1) % Row 10 \SetRowColor{LightBackground} `intersect()` & Intersection \tn % Row Count 13 (+ 1) % Row 11 \SetRowColor{white} `setdiff()` & Difference \tn % Row Count 14 (+ 1) % Row 12 \SetRowColor{LightBackground} `\%in\%` & Membership \tn % Row Count 15 (+ 1) % Row 13 \SetRowColor{white} `pct=TRUE` & Percentage (*100) \tn % Row Count 17 (+ 2) % Row 14 \SetRowColor{LightBackground} `mean()` & Mean \tn % Row Count 18 (+ 1) % Row 15 \SetRowColor{white} `median()` & Median \tn % Row Count 19 (+ 1) % Row 16 \SetRowColor{LightBackground} `sd()` & Standar deviation \tn % Row Count 21 (+ 2) % Row 17 \SetRowColor{white} `quantile()` & Quantiles \tn % Row Count 22 (+ 1) % Row 18 \SetRowColor{LightBackground} `quantile(df\$col, \{\{nl\}\}seq(0, 1, 0.1))` & Percentiles \tn % Row Count 24 (+ 2) % Row 19 \SetRowColor{white} `cor()` & Correlation between variables \tn % Row Count 26 (+ 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}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`summary(df)`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Gives you info about all the columns, (min, max, median, mean, 1n3Q)} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`head(df)`} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Gives you the first 6 lines by default, you can change it.} \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`tail(df)`} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Gives you the last 6 lines by default, you can change it.} \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`dim(df)`} \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Gives you the dimensions of your df} \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`str(df)`} \tn % Row Count 12 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Gives you like a list with the variables} \tn % Row Count 13 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`barplot(t)`} \tn % Row Count 14 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Creates a barplot, not the way we are going to do them} \tn % Row Count 16 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`length ( )`} \tn % Row Count 17 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Returns the length} \tn % Row Count 18 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Logical operators:}}} \tn % Row Count 19 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`all( )`} \tn % Row Count 20 (+ 1) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{TRUE if all elements are true} \tn % Row Count 21 (+ 1) % Row 17 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`any( )`} \tn % Row Count 22 (+ 1) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{TRUE if any of the elements is true} \tn % Row Count 23 (+ 1) % Row 19 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{To repeat things}}} \tn % Row Count 24 (+ 1) % Row 20 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`rep()`} \tn % Row Count 25 (+ 1) % Row 21 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`rep( \_ , times=\#)`} \tn % Row Count 26 (+ 1) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{You repeat that same thing the numbers you asked} \tn % Row Count 27 (+ 1) % Row 23 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`rep( \_ , each= \#)`} \tn % Row Count 28 (+ 1) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{You repeat each thing \# times} \tn % Row Count 29 (+ 1) % Row 25 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`sum()`} \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Functions (cont)}} \tn % Row 26 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Sum of vector elements} \tn % Row Count 1 (+ 1) % Row 27 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`seq()` or `\_:\_`} \tn % Row Count 2 (+ 1) % Row 28 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Generates a sequence} \tn % Row Count 3 (+ 1) % Row 29 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`cumsum()`} \tn % Row Count 4 (+ 1) % Row 30 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Cumulative sum in each position} \tn % Row Count 5 (+ 1) % Row 31 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`diff()`} \tn % Row Count 6 (+ 1) % Row 32 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Like cumsum but substracting} \tn % Row Count 7 (+ 1) % Row 33 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`nchar()`} \tn % Row Count 8 (+ 1) % Row 34 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Counts the number of characters in each position} \tn % Row Count 9 (+ 1) % Row 35 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`grep("\_", vector,ignore.case=TRUE)`} \tn % Row Count 10 (+ 1) % Row 36 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Pattern Matching and Replacement -\textgreater{} returns position} \tn % Row Count 12 (+ 2) % Row 37 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`grepl("\_",vector,ignore.case=TRUE)`} \tn % Row Count 13 (+ 1) % Row 38 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Pattern Matching and Replacement-\textgreater{} returns logical vector} \tn % Row Count 15 (+ 2) % Row 39 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`gsub("","",\_, ignore.case=FALSE)`} \tn % Row Count 16 (+ 1) % Row 40 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{For replacement, first what you want to put out, the what you want to put, and then where.} \tn % Row Count 18 (+ 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}{Data filtering and reordering}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{You can use logical conditions, they can be used in two forms:}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{- By creating a logical vector and applying it} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`logical\_vector \textless{}- c(TRUE, FALSE, TRUE, FALSE) \{\{nl\}\}products\_stock{[}logical\_vector{]}`} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{-By just applying the logical condition} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`vector{[}c(TRUE, FALSE, TRUE, TRUE, FALSE){]}`} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{- With the function `which()` returns the positions} \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{You can put conditions:}}} \tn % Row Count 10 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{- Either inside the line: `df{[}df\$col1 \textless{} 25, {]}`} \tn % Row Count 11 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{- Or with function subset()} \tn % Row Count 12 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`subset( df, column name with the condition)`} \tn % Row Count 13 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{With subset you can put more than one condition with the command `\&`} \tn % Row Count 15 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Reordering:}}} \tn % Row Count 16 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{- `sort ( \_, decreasing=FALSE)` by default decreasing is false and you can omit it. It rearrange the vector} \tn % Row Count 19 (+ 3) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{- ` order ( \_, decreasing=FALSE)`by default decreasing is false and you can omit it. It just gives the positions where they should be} \tn % Row Count 22 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}