Introduction to R Programming

Jared Knowles and Justin Meyer

Wi-Fi

Outline

We hope to move through the following topics in the next two hours.

Materials

If you don't have it already, now might be a good time to install R and RStudio to get started. Sector 67 computers already have these installed.

Installing R on Windows

Installation Tips

Next Install packages

First, watch this tutorial

Install Packages

Install Packages (2)

install_new<-function(mypkg){
  if (mypkg %in% installed.packages()) cat("Package already installed")
  else{cat("Package not found, so installing with dependencies... /n
           Press CTRL C to abort.")
    Sys.sleep(5)
    install.packages(mypkg,repos="http://cran.wustl.edu/")
}
}

install_new('plyr')
install_new('lmtest')
install_new('ggplot2')
install_new('gridExtra')
install_new('stringr')
install_new('knitr')
install_new('quantreg')
install_new('zoo')
install_new('xtable')
install_new('lme4')
install_new('caret')

References and Resources for the Previous Section

Overview

R

Why Use R

R Advantages Continued

R Can Compliment Other Tools

R's Drawbacks

R Vocabulary

Components of an R Setup

Self-help

Let's Look at RStudio

R As A Calculator

2 + 2  # add numbers
[1] 4
2 * pi  #multiply by a constant
[1] 6.283
7 + runif(1, min = 0, max = 1)  #add a random variable
[1] 7.457
4^4  # powers
[1] 256
sqrt(4^4)  # functions
[1] 16

Arithmetic Operators

2 + 2
[1] 4
2/2
[1] 1
2 * 2
[1] 4
2^2
[1] 4
2 == 2
[1] TRUE
23%/%2
[1] 11
23%%2
[1] 1

Other Key Symbols

foo <- 3
foo
[1] 3
1:10
 [1]  1  2  3  4  5  6  7  8  9 10
# it increments by one
a <- 100:120
a
 [1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
[18] 117 118 119 120

Comments in R

# Something I want to keep from R
# Like my secret from the R engine
# Maybe intended for a human and not the computer
# Like: Look at this cool plot!

myplot(readSS,mathSS,data=df)

R Advanced Math

Using the Workspace

Using the Workspace (2)

x <- 5  #store a variable with <-
x  #print the variable
[1] 5
z <- 3
ls()  #list all variables
[1] "a"   "foo" "x"   "z"  
ls.str()  #list and describe variables
a :  int [1:21] 100 101 102 103 104 105 106 107 108 109 ...
foo :  num 3
x :  num 5
z :  num 3
rm(x)  # delete a variable
ls()
[1] "a"   "foo" "z"  

R as a Language

  1. Case sensitivity matters
a <- 3
A <- 4
print(c(a, A))
[1] 3 4
  1. What happens if I type print(a,A)?

c is our friend

A <- c(3, 4)
print(A)
[1] 3 4

Language

a <- runif(100)  # Generate 100 random numbers
b <- runif(100)  # 100 more
c <- NULL  # Setup for loop (declare variables)
for (i in 1:100) {
    # Loop just like in Java or C
    c[i] <- a[i] * b[i]
}
d <- a * b
identical(c, d)  # Test equality
[1] TRUE

More Language Bugs Features

Objects

summary(df[, 28:31])  #summary look at df object
   schoollow         readSS        mathSS           proflvl    
 Min.   :0.000   Min.   :252   Min.   :210   advanced   : 788  
 1st Qu.:0.000   1st Qu.:430   1st Qu.:418   basic      : 523  
 Median :0.000   Median :495   Median :480   below basic: 210  
 Mean   :0.242   Mean   :496   Mean   :483   proficient :1179  
 3rd Qu.:0.000   3rd Qu.:562   3rd Qu.:543                     
 Max.   :1.000   Max.   :833   Max.   :828                     
summary(df$readSS)  #summary of a single column
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    252     430     495     496     562     833 

-The $ says to look for object readSS in object df

Graphics too

library(ggplot2) # Load graphics Package
library(eeptools)
qplot(readSS,mathSS,data=df,geom='point',alpha=I(0.3))+theme_dpi()+
  opts(title='Test Score Relationship')+
  geom_smooth()