Created
December 30, 2019 14:52
-
-
Save jonathanvoelkle/db44bbdac590aa468d925443e44a060a to your computer and use it in GitHub Desktop.
r cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass[10pt,landscape]{article} | |
\usepackage{multicol} | |
\usepackage{calc} | |
\usepackage{ifthen} | |
\usepackage[landscape]{geometry} | |
\usepackage{epsfig} | |
\ifthenelse{\lengthtest { \paperwidth = 11in}} | |
{ \geometry{top=.5in,left=.5in,right=.5in,bottom=.5in} } | |
{\ifthenelse{ \lengthtest{ \paperwidth = 297mm}} | |
{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} } | |
{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} } | |
} | |
\pagestyle{empty} | |
\makeatletter | |
\renewcommand{\section}{\@startsection{section}{1}{0mm}% | |
{-1ex plus -.5ex minus -.2ex}% | |
{0.5ex plus .2ex}%x | |
{\normalfont\large\bfseries}} | |
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}% | |
{-1explus -.5ex minus -.2ex}% | |
{0.5ex plus .2ex}% | |
{\normalfont\normalsize\bfseries}} | |
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}% | |
{-1ex plus -.5ex minus -.2ex}% | |
{1ex plus .2ex}% | |
{\normalfont\small\bfseries}} | |
\makeatother | |
% Define BibTeX command | |
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em | |
T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}} | |
% Don't print section numbers | |
\setcounter{secnumdepth}{0} | |
\setlength{\parindent}{0pt} | |
\setlength{\parskip}{0pt plus 0.5ex} | |
% ----------------------------------------------------------------------- | |
\usepackage{makecell} | |
\setlength{\tabcolsep}{1ex} | |
% ----------------------------------------------------------------------- | |
\begin{document} | |
\setlength{\premulticols}{1pt} | |
\setlength{\postmulticols}{1pt} | |
\setlength{\multicolsep}{1pt} | |
\setlength{\columnsep}{2em} | |
\hspace{-2em} | |
\begin{tabular}{l} | |
% \vspace{0cm}\epsfig{file=../licence/by-sa,width=24mm}\\ | |
\end{tabular}\vspace{-2mm} | |
\hfill | |
\hfill | |
\Large{\textbf{R}} | |
\hfill | |
\hfill | |
\scriptsize | |
\today{} --- | |
\\ | |
\hrule~\\ | |
\raggedright | |
\footnotesize | |
\begin{multicols}{3} | |
Implementations are in \verb!.R! files, notebooks are in \verb!.Rmd! | |
files.\\ | |
Everything after \verb!#! is seen as a comment by R.\\ | |
R is case sensitive, and only \verb!A-Za-z0-9! should be used.\\ | |
Commands are separated either by \verb!;! or a newline, and can be grouped by \verb!{!...\verb!}! \\ | |
To quit the R programm the command is \verb!q()! | |
\subsection{Help} | |
\subsubsection{Accessing the help files} | |
\begin{tabular}{ll} | |
\verb!?mean! & get help for particular function \\ | |
\verb!help.search('weighted mean')! & search the help files \\ | |
\verb!help(package = 'MASS')! & find help for package | |
\end{tabular} | |
\subsubsection{More about a object} | |
\begin{tabular}{ll} | |
\verb!str(hills)! & get summary of objects structure \\ | |
\verb!class(hills)! & find the class an object belongs to | |
\end{tabular} | |
\subsection{Packages} | |
\begin{tabular}{ll} | |
\verb!install.packages('MASS')! & download and install a package \\ | |
\verb!library(MASS)! & load the package into session, \\ | |
& making its functions available \\ | |
\verb!MASS::forbes! & using a function of a package \\ | |
\verb!data(hills)! & load a built-in dataset | |
\end{tabular} | |
\subsection{Working Directory} | |
\begin{tabular}{ll} | |
\verb!getwd()! & get the current working directory \\ | |
\verb!setwd('C://me/docs')! & change the current working directory | |
\end{tabular} | |
\subsection{Data types} % TODO | |
\begin{tabular}{lll} | |
\verb!logical! & Boolean, takes two values: \verb!TRUE! or \verb!FALSE! \\ | |
\verb!numerix! & Has two modes, \verb!integer! and \verb!double! \\ | |
\verb!complex! & \verb!1i! is used for \( i \) \\ | |
\verb!character! & Are enterd using either \verb!'! or \verb!"!\\ | |
% \verb!raw! & \\ | |
\end{tabular} | |
\subsection{Data Structures} | |
R operates on \textit{data structures}, which are set by \verb!<-! or \verb!=! | |
\begin{tabular}{ll} | |
\verb!x <- c(10.9, 5.6, 6.4)! & combine three numbers to a vector | |
\end{tabular} | |
\subsection{Vectors} | |
\subsubsection{Creating Vectors} | |
\begin{tabular}{lll} | |
\verb!c(2,4,6)! & \verb!2 4 6! & join elements into vector \\ | |
\verb!2:6! & \verb!2 3 4 5 6! & an integer sequence \\ | |
\verb!seq(2,3,by=0.5)! & \verb!2.0 2.5 3.0! & a double sequence \\ | |
\verb!rep(1:2, times=3)! & \verb!1 2 1 2 1 2! & repeat vector \\ | |
\verb!rep(1:2, each=3)! & \verb!1 1 1 2 2 2! & repeat elements of vector | |
\end{tabular} | |
\subsubsection{Vector Functions} | |
\begin{tabular}{ll} | |
\verb!sort(x)! & returns \verb!x! sorted \\ | |
\verb!table(x)! & see counts of values \\ | |
\verb!rev(x)! & returns \verb!x! reversed \\ | |
\verb!unique(x)! & see unique values \\ | |
\end{tabular} | |
\subsubsection{Selecting Vector Elements} | |
\begin{tabular}{ll} | |
\verb!x[4]! & the fourth element \\ | |
\verb!x[-4]! & all but the fourth \\ | |
\verb!x[2:4]! & elements two to four \\ | |
\verb!x[-(2:4)]! & all elements except two to four \\ | |
\verb!x[c(1, 5)]! & elements one and five \\ | |
\verb!x[x == 10]! & elements which are equal to \verb!10! \\ | |
\verb!x[x < 0]! & elements less than \verb!0! \\ | |
\verb!which(x < 2)! & values of elements less than \verb!2! \\ | |
\verb!x[x %in% c(1,2,5)]! & elemts that are in the set \verb!1,2,5! \\ | |
\verb!x['apple']! & element with name \verb!'apple'! | |
\end{tabular} | |
\subsection{Programming} | |
\subsubsection{For loop} | |
\begin{tabular}{ll} | |
for (variable in sequence) \{ & \verb!for (i in 1:4) {! \\ | |
\quad do something & \verb! j <- i + 10 ! \\ | |
\quad do more stuff & \verb! print(j)! \\ | |
\} & \verb!}! | |
\end{tabular} | |
\subsubsection{While Loop} | |
\begin{tabular}{ll} | |
while (condition) \{ & \verb!while(i < 5) {! \\ | |
\quad do something & \verb! print(i)! \\ | |
\quad do more things & \verb! i <- i + 1! \\ | |
\} & \verb!}! | |
\end{tabular} | |
\subsubsection{If Statements} | |
\begin{tabular}{ll} | |
if (condition) \{ & \verb!if (i > 3) {! \\ | |
\quad do something & \verb! print('Yes')! \\ | |
\} else \{ & \verb!} else {! \\ | |
\quad do something different & \verb! print('No')! \\ | |
\} & \verb!}! | |
\end{tabular} | |
\subsubsection{Functions} | |
\begin{tabular}{ll} | |
function\_name <- function(var) \{ & \verb!square <- function (x) {! \\ | |
\quad do something & \verb! squared <- x*x! \\ | |
\quad return (new\_variable) & \verb! return(squared)! \\ | |
\} & \verb!}! | |
\end{tabular} | |
\subsubsection{Conditions} | |
\begin{tabular}{ll} | |
\verb!a == b! & Are equal \\ | |
\verb!a != b! & Not equal \\ | |
\verb!a > b! & Greater than \\ | |
\verb!a < b! & Less than \\ | |
\verb!a >= b! & Greater than or equal \\ | |
\verb!a <= b! & Less than or equal \\ | |
\verb!is.na(a)! & Is missing \\ | |
\verb!is.null(a)! & In null | |
\end{tabular} | |
\subsection{Reading and Writing Data} | |
% See also \verb!readr! package \\ | |
\begin{tabular}{ll} | |
\verb!df <- read.table('file.txt')! & Read and write \\ | |
\verb!write.table(df, 'file.txt')! & \hfill a delimited text file \\ | |
\verb!df <- read.csv('file.csv')! & Read and write a \\ | |
\verb!write.csv(df, 'file.txt')! & \hfill comma separated value file \\ | |
\verb!load('file.Rdata')! & Read and write R data file \\ | |
\verb!save(df, file='file.Rdata')! & \hfill | |
\end{tabular} | |
\subsection{Types} | |
Converting between common data types in R. Can always go from a higher value in the table to a lower value | |
\begin{tabular}{lll} | |
\verb!as.logical(x)! & Boolean value \\ | |
\verb!as.numeric(x)! & Integers of floating point numbers \\ | |
\verb!as.charater(x)! & Character strings \\ | |
\verb!as.factor(x)! & Character strings with preset levels. \\ | |
& Needed for some statistical models \\ | |
\end{tabular} | |
You can test if a element is of a given type using \verb!is.logical(x)!, \verb!is.integer(x)!, \verb!is.double(x)! and \verb!is.character(x)!. \verb!length(x)! determines the length, \verb!typeof(x)! the type of a vector | |
\subsection{Math Functions} | |
\begin{tabular}{ll} | |
\verb!1+2! & Addition \\ | |
\verb!2-3! & Subtraction \\ | |
\verb!3*4! & Multiplication \\ | |
\verb!4/5! & Division \\ | |
\verb!5%/%6! & Integer divison \\ | |
\verb!6^7! & Exponentiation \\ | |
\verb!7%%8! & Modulus \\ | |
\verb!sqrt(x)! & Square root \\ | |
\verb!floor(x)! & Floor function \\ | |
\verb!log(x)! & Natural log \\ | |
\verb!exp(x)! & Exponential \\ | |
\verb!sin(x)! & Sinus function \\ | |
\verb!max(x)! & Largest element \\ | |
\verb!min(x)! & Smallest element \\ | |
\verb!round(x, n)! & Round to \verb!n! decimal places \\ | |
\verb!signif(x, n)! & Round to \verb!n! significant figures \\ | |
\verb!cor(x, y)! & Correlation \\ | |
\verb!sum(x)! & Sum \\ | |
\verb!cumsum(x)! & Cumulative sum \\ | |
\verb!prod(x)! & Product \\ | |
\verb!mean(x)! & Mean \\ | |
\verb!median(x)! & Median \\ | |
\verb!quantile(x)! & Percentage quantiles \\ | |
\verb!rank(x)! & Rank of elements \\ | |
\verb!var(x)! & The variance \\ | |
\verb!sd(x)! & The standard deviation | |
\end{tabular} | |
\subsection{Variable Assignment} | |
\begin{tabular}{l} | |
\verb!> a <- 'apple'! \\ | |
\verb!> a! \\ | |
\verb![1] 'apple'! | |
\end{tabular} | |
\subsection{The Environment} | |
\begin{tabular}{ll} | |
\verb!ls()! & List all the variables in the environment \\ | |
\verb!rm(x)! & Remove \verb!x! from the environment \\ | |
\verb!rm(list = ls())! & Remove all variables from the environment | |
\end{tabular} | |
\subsection{Matrices} | |
\verb!m <- matrix(x, nrow=3, ncol=3)! \ Create a matrix from \verb!x! | |
\begin{tabular}{ll} | |
\verb!m[2, ]! & Select the second row \\ | |
\verb!m[ , 1]! & Select the second column \\ | |
\verb!m[2, 3]! & Select the second element in the third column \\ | |
\verb!t(m)! & Transpose \\ | |
\verb!m %*% n! & Matrix mulitplication \\ | |
\verb!solve(m, n)! & Find \(x\) in \(m*x=n\) \\ | |
\verb!m.inv! & Inverts matrix | |
\end{tabular} | |
\subsection{Lists} | |
\verb!l <- list(x=1:5, y=c('a','b')! A list is a collection of elements which can be of different types | |
\begin{tabular}{ll} | |
\verb!l[[2]]! & Second element of l \\ | |
\verb!l[1]! & New list with only the first element \\ | |
\verb!l$x! & Elements named x \\ | |
\verb!l['y']! & New list with only element named y | |
\end{tabular} | |
\subsection{Data Frames} | |
% See also \verb!dplyr! package \\ | |
\verb!df <- data.frame(x=1:3, y=c('a','b'))! A special case of a list where all elements are the same length \\ | |
Both the matrix and list subsetting can be used \\ | |
\begin{tabular}{ll} | |
\verb!View(df)! & See the full data frame \\ | |
\verb!head(df)! & See the first 6 rows \\ | |
\verb!nrow(df)! & Number of rows \\ | |
\verb!ncol(df)! & Number of columns \\ | |
\verb!dim(df)! & Number of columns and rows \\ | |
\verb!cbind()! & Bind columns \\ | |
\verb!rbind()! & Bind rows | |
\end{tabular} | |
\subsection{Strings} | |
% Also see the \verb!stringr! package \\ | |
\begin{tabular}{ll} | |
\verb!paste(x, y, sep=' ')! & Join multiple vectors together \\ | |
\verb!paste(x, collapse=' ')! & Join elements of a vector together \\ | |
\verb!grep(regex, x)! & Find regular expression matches in x \\ | |
\verb!gsub(regex, repl, x)! & Replace matches in x with a string \\ | |
\verb!toupper(x)! & Convert to uppercase \\ | |
\verb!tolower(x)! & Convert to lowercase \\ | |
\verb!nchar(x)! & Number of characters in a string | |
\end{tabular} | |
\subsection{Factors} | |
\begin{tabular}{ll} | |
\verb!factor(x)! & Turn a vector into a factor. Can set the \\ | |
& levels of the factor and the order \\ | |
\verb!cut(x, breaks=4)! & Turn a numeric vector into a \\ | |
& factor by `cutting' into sections | |
\end{tabular} | |
\subsection{Statisics} | |
\begin{tabular}{ll} | |
\verb!lm(y ~ x, data=df)! & Linear model \\ | |
\verb!glm(y ~ x, data=df)! & Generalised linear model \\ | |
\verb!summary! & Get more detailed information out a model \\ | |
\verb!t.test(x, y)! & Perform a t-test for a difference between means \\ | |
\verb!pairwise.t.test! & Perform a t-test for a paired data \\ | |
\verb!prop.test! & Test for a difference between proportions \\ | |
\verb!aov! & Analysis of the variance | |
\end{tabular} | |
\subsection{Distributions} | |
\begin{tabular}{lcccc} | |
& \makecell{Random \\ Variates} & \makecell{Density \\ Function} & \makecell{Cumulative \\ Distribution} & \makecell{Quantile} \\ | |
Normal & \verb!pnorm! & \verb!dnorm! & \verb!pnorm! & \verb!qnorm! \\ | |
Poisson & \verb!rpois! & \verb!dpois! & \verb!ppois! & \verb!qpois! \\ | |
Binomial & \verb!rbinom! & \verb!dbinom! & \verb!pbinom! & \verb!qbinom! \\ | |
Uniform & \verb!runif! & \verb!dunif! & \verb!punif! & \verb!qunif! | |
\end{tabular} | |
\subsection{Plotting} | |
\begin{tabular}{ll} | |
\verb!plot(x)! & Values of x in order \\ | |
\verb!plot(x, y)! & Values of x against y \\ | |
\verb!hist(x)! & Histogramm of x | |
\end{tabular} | |
\end{multicols} | |
\end{document} | |
\subsection{Misc} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment