Introduction to R

What is R and Why Should You Use It?

Maj Jason Freels

Air Force Institute of Technology

OVERVIEW

In This Presentation...

WHAT IS R?

In a Nutshell, R is...

IEEE Ranking Of Top Programming Languages - 2015 (click the image)

Where Did R Come From?

WHY SHOULD I USE R? - HUMAN EFFICIENCY

Human efficiency vs. Machine efficiency

Example of Human Efficiency with R

Fuel Efficiency Example

Think About what you want...

Data Set in Interactive Table

Plots to Support Conclusions (with code for better reproducibility)

Table of results from your analysis

\[ \begin{array}{rrrrr} \hline & Estimate & Std. Error & t value & Pr(>|t|) \\ \hline (Intercept) & 12.30 & 18.72 & 0.66 & 0.52 \\ cyl & -0.11 & 1.05 & -0.11 & 0.92 \\ disp & 0.01 & 0.02 & 0.75 & 0.46 \\ hp & -0.02 & 0.02 & -0.99 & 0.33 \\ drat & 0.79 & 1.64 & 0.48 & 0.64 \\ wt & -3.72 & 1.89 & -1.96 & 0.06 \\ qsec & 0.82 & 0.73 & 1.12 & 0.27 \\ vs & 0.32 & 2.10 & 0.15 & 0.88 \\ am & 2.52 & 2.06 & 1.23 & 0.23 \\ gear & 0.66 & 1.49 & 0.44 & 0.67 \\ carb & -0.20 & 0.83 & -0.24 & 0.81 \\ \hline \end{array} \]

ADVANTAGES AND DISADVANATAGES OF USING R

Advantages

Disadvantages

How R is different

UNDERSTANDING THE R ENVIRONMENT

Basic Structure of R

Analogy for Understanding R's Basic Structure

Let's Examine What Happens When The Following Code Is Run

my.table <- xtable::xtable(mtcars)
print(my.table)

R Packages

...we go from this...

...to this...

INTERACTING WITH THE R ENVIRONMENT

Everything in R is an OBJECT...Almost

The Process of R Scripting - Step 1



Assigning names to objects - R is CasE sENsItiVE!!

var <- 5  ### Left assignment
200 -> Var ### Right assignment
meaning.of_life = 42 ### Or equal sign
var; Var; meaning.of_life

[1] 5 [1] 200 [1] 42

HELPFUL FUNCTIONS

Functions to access R object properties

mode(object.name)  ### Returns an object's mode 
class(object.name) ### Returns an object's class
attributes(object.name) ### Returns the attributes associated with an object
str(object.name) ### Returns the complete list of properties assigned to an object
str( ) is every R users best friend

Functions to manage active objects

ls( ) ### Returns a list of active objects in the current working environment
rm( ) ### Removes an object from the current working environment
rm(list = ls()) ### Removes all objects from the current working environment (use carefully)

Functions to manage your local file structure

getwd( ) ### Returns the location of the current working directory
setwd("C:/Users/Desktop") ### Reassigns the location of the working directory
file.choose( ) ### Opens a new file explorer window to choose a file

Help Resources Within R

?cos ### Searches the local package library for "cos"
??xyz ### Searches the full R documentation for "xyz" - also help(xyz)
vignette() ### Lists "how-to" demos available for each package in the library
help.search("t.test") ### Provides a categorized search of R documentation for "t.test"

Online Resources for Help

NOW YOU KNOW ENOUGH TO BE DANGEROUS