\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{bwaldo} \pdfinfo{ /Title (r-basics.pdf) /Creator (Cheatography) /Author (bwaldo) /Subject (R Basics 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}{378735} \definecolor{LightBackground}{HTML}{F2F7F2} \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 Basics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{bwaldo} via \textcolor{DarkBackground}{\uline{cheatography.com/35095/cs/11018/}}} \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}bwaldo \\ \uline{cheatography.com/bwaldo} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 27th February, 2017.\\ Updated 27th February, 2017.\\ 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}{R Objects and Attributes}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Vectors}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{ The c() function can be used to create vectors of objects }}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{x \textless{}- c(0.5, 0.6) \#\# numeric} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{x \textless{}- c(TRUE, FALSE) \#\# logical} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{x \textless{}- c(T, F) \#\# logical} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{x \textless{}- c("a","b","c") \#\# character} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{x \textless{}- 9:29 \#\# interger} \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{x \textless{}- c(l+0i, 2+4i) \#\# complex} \tn % Row Count 8 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Using the vector() function }}} \tn % Row Count 9 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} x \textless{}- vector("numeric", length = 10)} \tn % Row Count 10 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{When different objects are mixed in a vector, coercion occurs so that every element in the vector is of the same class}}} \tn % Row Count 13 (+ 3) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{y \textless{}- c(1.7,"a") \#\# character} \tn % Row Count 14 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{y \textless{}- c(TRUE, 2) \#\# numeric} \tn % Row Count 15 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{y \textless{}- c("a", TRUE) \#\# characte} \tn % Row Count 16 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Objects can be explicitly coerced from one class to another using the "as.*" functions if available}}} \tn % Row Count 19 (+ 3) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} x \textless{}- 0:6 \#\# create a sequence of 0-6 \textgreater{} class(x) \#\# class of the object {[}1{]} "integer"} \tn % Row Count 21 (+ 2) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} as.numeric(x) \#\# change the class of the object to numeric {[}1{]} 0 1 2 3 4 5 6} \tn % Row Count 23 (+ 2) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} as.logical(x) \#\# change the class of the object to logical {[}1{]} FALSE TRUE TRUE TRUE TRUE TRUE TRUE} \tn % Row Count 26 (+ 3) % Row 17 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} as.character(x) \#\# change the class of the object to character {[}1{]} "0" "1" "2" "3" "4" "5" "6"} \tn % Row Count 28 (+ 2) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} as.complex(x) {[}1{]} 0+0i 1+0i 2+0i 3+0i 4+0i 5+0i 6+0i} \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Vectors (cont)}} \tn % Row 19 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Nonsensical coercion results in NA's}}} \tn % Row Count 1 (+ 1) % Row 20 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} x\textless{}- c("a","b","c") \textgreater{} as.numeric(x) {[}1{]} NA NA NA Warning message: NAs introduced by coercion} \tn % Row Count 3 (+ 2) % Row 21 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} as.logical(x) {[}1{]} NA NA NA} \tn % Row Count 4 (+ 1) % Row 22 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} as.complex(x) {[}1{]} NA NA NA} \tn % Row Count 5 (+ 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}{Reading Tabular Data}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{read.table, read.csv * Tabular data\{\{nl\}\} readLines * Reading lines of a text file\{\{nl\}\} source * Reading in R code files (inverser of dump)\{\{nl\}\} dget * Reading in R code files (inverse of dput)\{\{nl\}\} load * Reading in saved workspaces unserialize * Reading in single R objects in binary form\{\{nl\}\}} \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\emph{Analogous functions for WRITING data to files}}} \tn % Row Count 8 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{write.table(), writeLines(), dump(), dput(), save(), serialize()} \tn % Row Count 10 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{The read.table function is one of the most commonly used functions for reading data. Below are a few arguments}}} \tn % Row Count 13 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{file * the name of a file or a connection header * logical indicating if the file has a header line sep * a string indicating how the columns are separated colClasses * a character verctor indicating the class of each column in the dataset nrows * the number of rows in the dataset comment.char * a character string indicating the comment character skip * the number of lines to skip from the beginning stringsAsFactors * should character variables be coded as factors? defaults to true} \tn % Row Count 23 (+ 10) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{For small to moderatly sized datasets you can call read.table without specifying any other arguments.}}} \tn % Row Count 26 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{ data \textless{}- \seqsplit{read.table("sometable.txt")} }} * R will automatically\{\{nl\}\} * skips lines that being with a "\#"\{\{nl\}\} * figure out how many rows there are (and how much memory needs to be allocated)\{\{nl\}\} * figure what type of variable is in each column of hte table telling R all these things\{\{nl\}\} * directly makes R run faster and more efficiently.\{\{nl\}\} * read.csv is idental to read.table EXCEPT that the default separator is a comma (,)\{\{nl\}\}} \tn % Row Count 35 (+ 9) \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}}{{\bf{Lists - Lists are a special type of vector that can contain elements of different classes. Lists are a very IMPORTANT data type in R.}}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} x \textless{}- list(1,"a",TRUE, 1 +4i) \#\#list function} \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} x {[}{[}1{]}{]} \#\#vector element 1 {[}1{]} 1 {[}{[}2{]}{]} \#\#vector element 2 {[}1{]} "a" {[}{[}3{]}{]} \#\#vector element 3 {[}1{]} TRUE {[}{[}4{]}{]} \#\#vector element 4 {[}1{]} 1+4i} \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Matrices}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Matrices are vectors with a dimension attribute. The dimension attribute is itself an integer vector of length 2(nrow,ncol)}}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} m \textless{}- matrix(nrow = 2, ncol = 3) \#matrix function} \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} m {[},1{]} {[},2{]} {[},3{]} {[}1,{]} NA NA NA {[}2,{]} NA NA NA} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} dim(m) {[}1{]} 2 3} \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} attributes(m) \$dim {[}1{]} 2 3} \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Matrices are constructed column-wise, so entries can be thought of starting in the "upper left" corner and running down the columns}}} \tn % Row Count 11 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} m \textless{}- matrix(1:6, nrow = 2, ncol = 3) \# create a matrix with a sequence of 1-6} \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} m {[},1{]} {[},2{]} {[},3{]} {[}1,{]} 1 3 5 {[}2,{]} 2 4 6} \tn % Row Count 15 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Matrices can also be created directly form vectors by adding a dimension attribute}}} \tn % Row Count 17 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} m \textless{}- 1:10 \# create a vector that is a sequence from 1-10 \textgreater{} m {[}1{]} 1 2 3 4 5 6 7 8 9 10} \tn % Row Count 19 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Adding the dim function but assigning and attribute to m, take this vector and transform it into a matrix that is 2 rows and 5 columns}}} \tn % Row Count 22 (+ 3) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} dim(m) \textless{}- c(2,5)} \tn % Row Count 23 (+ 1) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} m {[},1{]} {[},2{]} {[},3{]} {[},4{]} {[},5{]} {[}1,{]} 1 3 5 7 9 {[}2,{]} 2 4 6 8 10} \tn % Row Count 25 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{Matrices can be created by column-binding cbind() or row-binding rbind()} \tn % Row Count 27 (+ 2) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} x\textless{}-1:3 \textgreater{} y\textless{}-10:12 \textgreater{} cbind(x,y) x y {[}1,{]} 1 10 {[}2,{]} 2 11 {[}3,{]} 3 12 \textgreater{} rbind(x,y) {[},1{]} {[},2{]} {[},3{]} x 1 2 3 y 10 11 12} \tn % Row Count 30 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Reading Large Tables}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{A quick and dirty way to figure out the classes of each column is the following:}} * initial \textless{}- \seqsplit{read.table("datatable.txt"}, nrows = 100)\{\{nl\}\} * classes \textless{}- sapply(initial, class)\{\{nl\}\} * tabALL \textless{}- \seqsplit{read.table("datatable.txt"}, colClasses = classes)\{\{nl\}\} * Set nrows. This doesn't make R run faster but it helps with memory usage\{\{nl\}\}} \tn % Row Count 7 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Textual Data Formats}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{* dput(): writes R code to which can be used to reconstruct a R object.\{\{nl\}\} * dget(): single R objects\{\{nl\}\} * dump(): similar to dget but can be used on multiple R objects.\{\{nl\}\}} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{dput() example}}\{\{nl\}\} \textgreater{} y \textless{}- data.frame(a=1,b="a")\{\{nl\}\} \textgreater{} dput(y) \# output to console\{\{nl\}\} structure(list(a = 1, b = structure(1L, .Label = "a", class = "factor"))\{\{nl\}\} , .Names = c("a", "b"), row.names = c(NA, -1L), class = "data.frame")\{\{nl\}\} \textgreater{} dput(y, file ="y.R") \# save to a file named y.R\{\{nl\}\} \textgreater{} new.y \textless{}- dget("y.R") \# get the file\{\{nl\}\} \textgreater{} new.y\{\{nl\}\} a b\{\{nl\}\} 1 1 a\{\{nl\}\}} \tn % Row Count 12 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{dump() example}}\{\{nl\}\} \newline \textgreater{} x \textless{}- "foo"\{\{nl\}\} \newline \textgreater{} y \textless{}- data.frame(a=1,b="a")\{\{nl\}\} \newline \textgreater{} dump(c("x","y"), file = "data.R") \# paSS the objects x and y and create and store in data.R\{\{nl\}\} \newline \textgreater{} rm(x,y) \# remove them from R\{\{nl\}\} \newline \textgreater{} source("data.R") \# calls them back into R\{\{nl\}\} \newline \textgreater{} y\{\{nl\}\} \newline a b\{\{nl\}\} \newline 1 1 a\{\{nl\}\} \newline \textgreater{} x\{\{nl\}\} \newline {[}1{]} "foo"\{\{nl\}\}} \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}{Factors}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Factors are used to represent categorical data and can be unordered or ordered. One can think of a factor as an integer vector where each integer has a label}}} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} x \textless{}- factor(c("yes","yes", "no" , "yes", "no")) \textgreater{} x {[}1{]} yes yes no yes no Levels: no yes} \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} table(x) \#\#if you table x then it will tell how many of each value there are x no yes 2 3} \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} unclass(x) {[}1{]} 2 2 1 2 1 attr(,"levels") {[}1{]} "no" "yes"} \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{The order of the levels can be set using the levels argument to factor(). This can be important in linear modelling because the first level is used as the baseline level}}} \tn % Row Count 14 (+ 4) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{So in the example below we set the levels of c because default would put no first and we want yes first \textgreater{} x \textless{}- factor(c("yes","yes", "no" , "yes", "no"),levels = c("yes","no")) \textgreater{} x {[}1{]} yes yes no yes no Levels: yes no} \tn % Row Count 19 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Missing Values}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Missing values are denoted by NA or NaN for undefined mathematical operations.}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{* is.na() is used to test objects if they are NA\{\{nl\}\} * is.nan() is used to test for NaN\{\{nl\}\} * NA values have a class also, so there are interger NA, character NA etc\{\{nl\}\} * a NaN value is also NA but the converse is not true\{\{nl\}\}} \tn % Row Count 8 (+ 6) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} x \textless{}- c(1,2,NA,10,3) \textgreater{} x {[}1{]} 1 2 NA 10 3} \tn % Row Count 9 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} is.na(x) {[}1{]} FALSE FALSE TRUE FALSE FALSE} \tn % Row Count 10 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} is.nan(x) {[}1{]} FALSE FALSE FALSE FALSE FALSE} \tn % Row Count 11 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} x \textless{}- c(1,2,NaN,NA,4) \textgreater{} is.na(x) {[}1{]} FALSE FALSE TRUE TRUE FALSE} \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} is.nan(x) {[}1{]} FALSE FALSE TRUE FALSE FALSE} \tn % Row Count 14 (+ 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}{Names Attribute}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{R objects can also have names, which is very useful for writing readable code and self-describing objects}}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} x \textless{}- 1:3\{\{nl\}\} \textgreater{} names(x)\{\{nl\}\} NULL\{\{nl\}\} \textgreater{} names(x) \textless{}-c("foo","bar","norf")\{\{nl\}\} \textgreater{} x\{\{nl\}\} foo bar norf \{\{nl\}\} 1 2 3 \{\{nl\}\} \textgreater{} names(x)\{\{nl\}\} {[}1{]} "foo" "bar" "norf"\{\{nl\}\}} \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{List can also have names}}} \tn % Row Count 8 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} x \textless{}- list(a=1,b=2,c=3)\{\{nl\}\} \textgreater{} x\{\{nl\}\} \$a\{\{nl\}\} {[}1{]} 1\{\{nl\}\} \$b\{\{nl\}\} {[}1{]} 2\{\{nl\}\} \$c\{\{nl\}\} {[}1{]} 3\{\{nl\}\}} \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Matrices can have names}}} \tn % Row Count 12 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} m \textless{}- matrix(1:4, nrow=2, ncol=2) \#\# create a matrix sequence 1-4\{\{nl\}\} \textgreater{} dimnames(m) \textless{}- list(c("a","b"), c("c","d"))\{\{nl\}\} \textgreater{} m\{\{nl\}\} c d\{\{nl\}\} a 1 3\{\{nl\}\} b 2 4\{\{nl\}\}} \tn % Row Count 16 (+ 4) \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 Frames}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Data frames are used to store tabular data.}}\{\{nl\}\} * They are represented as a special type of list where every element of the list has to have the same length.\{\{nl\}\} * Each element of hte list can be thought of as a column and the length of each element of the list is the \# or rows.\{\{nl\}\} * Unlike matrices, data frames can store different classes of objects in each column (just like lists) matrices must have every element be the same class.\{\{nl\}\} * Data frames also have a special attribute called row.names\{\{nl\}\} * Data frames are usually created by calling read.table() or read.csv()\{\{nl\}\} * Can be converted to a matrix by calling data.matrix()\{\{nl\}\}} \tn % Row Count 14 (+ 14) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textgreater{} x \textless{}- data.frame(foo = 1:4, bar = c(T,T,F,F))\{\{nl\}\} \textgreater{} x\{\{nl\}\} foo bar\{\{nl\}\} 1 1 TRUE\{\{nl\}\} 2 2 TRUE\{\{nl\}\} 3 3 FALSE\{\{nl\}\} 4 4 FALSE\{\{nl\}\} \textgreater{} nrow(x)\{\{nl\}\} {[}1{]} 4\{\{nl\}\} \textgreater{} ncol(x)\{\{nl\}\} {[}1{]} 2\{\{nl\}\}} \tn % Row Count 19 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}