HMC R Bootcamp 2018

Jeho Park
May 28, 2018

HMC R Bootcamp 2018

DAY 1

Reference:

Some (i.e., a lot of) materials are adapted from the following websites:

(Do not click on the links–it will open a new )

Some Housekeeping Stuff

This R bootcamp is...

  • about R's programming aspects.
    • It's designed to help you start using and coding R.
  • not about Statistics.
    • I assume that you already have a basic knowledge of Statistics
  • for your summer research and beyond.
    • This bootcamp materials are used to help you get up to speed on using R as quickly as possible for your research activites

R Bootcamp Day 1 Agenda

  • Module 0: Introduction
    • About this bootcamp
    • Learning Objectives
    • About R
    • Getting ready (R environment setup)
  • Module 1: The Basics of R
    • General Stuff
    • RStudio
    • R Data Structure (R Objects)
    • Basic graphics

Day 1 Agenda (Cont.)

  • Module 2: Working with Data
    • (Almost) clean data (CSV files)
    • Data import and export (interactively)
    • Data cleaning
    • Subsetting
    • Matrix vs Data Frame vs Data Table
    • Attributes and missing values
    • Graphics (lattice and ggplot2)
    • Simple graph manipulations

Module 0: Introduction (15 min)

  • Goals and learning objectives
  • What is R?
  • What is not R?
  • Then Why R?
  • Getting ready for R Bootcamp

Bootcamp Goal:

To prepare YOU to use R to handle and analyze sample data of your choice.

Learning Objectives:

By the end of this two-day bootcamp, you will be able to:

  • Import data from a simple CSV file in R
  • Distinguish the differences between string variable and other R data types
  • Convert different data types back and forth
  • Create a loop using R’s for loop syntax
  • Create an if-else statement in R
  • Interpolate data points and use interpolating functions given
  • Restate the existing R script and explain what each block of the script is for
  • Create basic plots and scale x and y axis according to data points
  • Use subsetting methods

What is R?

  • R is a statical programming language/environment.
  • R is open source and free.
  • R is widely used.
  • R is cross-platform.
  • R is hard to learn (really?).

What is not R?

  • S: R's ancestor
  • S-Plus: Commercial; modern implementation of S
  • SAS: Commercial; widely used in the commercial analytics.
  • STATA: Commercial; widely used by economists.
  • SPSS: Commercial; easy to use; widely used in Social Science.
  • MATLAB: Commercial; can do some Stats.
  • Python: Also can do some Stats; one of the two mostly used programming languages in data science.

Then Why R?

  • R community is active and constantly growing
  • R is one of the most popular stat programming lang
  • R has tons of user generated libraries/packages
  • R code is easily shared with others
  • R is constantly improved

Then Why R?

  • R community is active and constantly growing
  • R is one of the most popular stat programming lang
  • R has tons of user generated libraries/packages
  • R code is easily shared with others
  • R is constantly improved
Further reading about R's popularity in science and engineering:
R moves up to 5th place in IEEE language rankings at https://www.r-bloggers.com/r-moves-up-to-5th-place-in-ieee-language-rankings/
https://spectrum.ieee.org/static/interactive-the-top-programming-languages-2017 (6th in 2017)

Then Why R?

IEEE Spectrum: Popular Programming Languages in 2017 Poplar Programming Languages

Then Why R?

  • R community is active and constantly growing
  • R is one of the most popular stat programming lang)
  • R has tons of user generated libraries/packages
  • R code is easily shared with others
  • R is constantly improved

Getting help online and offline

On the Internet:

Local Resources:

Module 1: The Basics of R (45 min)

  • R scripting environment
    • RStudio and its functionality
  • General stuff
  • Workspace of R and some calculations
  • R Objects (Vectors, Matrices, Lists, Data frames, and Factors)
  • Converting between different types of objects
  • Getting help online and offline

What is RStudio?

  • Integrated Development Environment for R
  • Nice combination of GUI and CLI
  • 4 main windows, tabs, etc
  • Version control: Git and VPN
  • R Markdown
  • R Presentation

Get ready

  • Let's open RStudio

What Can We Do with RStudio?

Take a look at RStudio panes and tabs

RStudio Project

  • RStudio project helps you to keep all the files associated with a project/work in one directory along with environment, history, version control (git) files.
  • Let's create a new RStudio project by

    File >> New Project…

  • Copy and paste the R command file and data files into the project directory. (RStudio limitation)

Getting Ready!

  • Check R
  • Check RStudio
  • Check RStudio project
  • Check slides
  • Check R command script

General Stuff

demo() # display available demos
demo(graphics) # try graphics demo
library() # show available packages on the computer
search() # show loaded packages
?hist # search for the usage of hist function
??histogram # search for package documents containing the word "histogram"

Workspace of R

R workspace stores objects like vecors, datasets and functions in memory (the available space for calculation is limited to the size of your computer's RAM).

a <- 5 # notice a in your Environment window
A <- "text" 
a
A
ls()
print(c(a,A))
print(a, A)

Look Ma, R can do Math!

1+1
2+runif(1,0,1)
2+runif(1,min=0,max=1)
3^2
3*3
sqrt(3*3) # comments
# comments are preceded by hash sign
#ignorehashtag!

Even More Math!

  • R can take integrals and derivatives, for example:

Numerical Integral of

\( \displaystyle\int_0^{\infty} \frac{1}{\sqrt{x}(x+1)}dx \)

# define the integrated function
integrand <- function(x) {1/(sqrt(x)*(x+1))} 
# integrate the function from 0 to infinity
integrate(integrand, lower=0, upper=Inf) 
3.141593 with absolute error < 2.7e-05

R Objects: Vectors

The most basic form of an R object.
A scalar value is a vector of length one.
A vector is an array object of the same type (homogeneous) data elements.

class(a)
class(A)
B <- c(a,A) # concatenation function
B # see the values
class(B) # why?
a <- rnorm(10)
a[3:5] <- NA # NA is a missing value
a

R Objects: Vectors (cont.)

R has five basic or “atomic” classes of objects:

  • character
  • numeric (real numbers)
  • integer
  • complex
  • logical (True/False)

A vector contains a set of data in any one of the atomic classes.

R Objects: Matrices

A matrix is a two-dimensional rectangular object of the same type (homogeneous) data elements.

mat <- matrix(rnorm(6), nrow = 3, ncol = 2) 
mat # a matrix
dim(mat) # dimension
t(mat) 
summary(mat) 

R Objects: Lists

A list is an object that can store different types of vectors.

list1 <- list(name=c("Joseph"), married=T, kids=2)
list1
list1$kids <- list1$kids+1
list1$kids
list2 <- list(numeric_data=a,character_data=A)
list2
all_list <- list(list1, list2)
all_list

R Objects: Data frames

A data frame is a list of vectors of equal length with possibly different types. It is used for storing rectangular data tables.

n <- c(2, 3, 5) # a vector 
s <- c("aa", "bb", "cc") # a vector
b <- c(TRUE, FALSE, TRUE) # a vector
df <- data.frame(n, s, b) # a data frame
df
class(df$s) # was a string vector but now a factor. 
mtcars # a built-in (attached) data frame
mtcars$mpg

R Objects: Data frames (cont.)

my_df <- data.frame(y1=rnorm(100),y2=rnorm(100), y3=rnorm(100))
head(my_df) # display first few lines of data
names(my_df) # display column names
summary(my_df) # output depends on the data types
plot(my_df)

R Objects: Factors

  • Factors are a special compoud object used to represent categorical data such as gender, social class, etc.
  • Factors have 'levels' attribute. They may be nominal or ordered.
v <- c("a","b","c","c","b")
x <- factor(v) # turn the character vector into a factor object
z <- factor(v, ordered = TRUE) # ordered factor
x
z
table(x)

Converting between different types

Use of the as() family of functions. Type as. and wait to see the list of as() functions.

integers <- 1:10
as.character(integers)
as.numeric(c('3.7', '4.8'))
indices <- c(1.7, 2.3)
integers[indices] # sometimes R is too generous
integers[0.999999999] # close to 1 but...
df <- as.data.frame(mat)
df

Getting help online and offline

Wrap-up exercise (15 min)

Basics
1) Using cars datasets, create a variable 'stop_mean' that contains mean stopping distance.
2) Create a matrix 'cars_mat' by converting the cars data frame.

Challenges
3) Install 'hflights' datasets.
4) Using hflights datasets, create a variable called 'x' that contains the mean flight arrival delay. (Hint: NA's)
5) Create a boolean (TRUE/FALSE) vector indicating whether the departure delay is shorter than the arrival delay.

Break (5 min)

Take a break!

Solutions and discussions (15 min)

1)

2)

3)

4)

5)

Module 2: Working with Data

  • Working with (almost) clean data
  • Data import and export (interactive way)
  • Subsetting
  • Using data frames vs. matrices

Working with (almost) clean data

  • Use read.table() to read text files into R
  • Try help document for read.table()

Data Import

  • read.csv() is a special case of read.table()
  • Data import from your local folder
cpds <- read.csv(file.path('.', 'data', 'cpds.csv'))
head(cpds) # good to look at a few lines
class(cpds) # data.frame
  • Data import from the Internet
data <- read.table(file="http://scicomp.hmc.edu/data/R/normtemp.txt", header=T)
tail(data)

Data Import (Cont.)

rta <- read.table("./data/RTADataSub.csv", sep = ",", head = TRUE)
dim(rta)
rta[1:5, 1:5]
class(rta)
class(rta$time) # what? let's see ?read.table more carefully
rta2 <- read.table("./data/RTADataSub.csv", sep = ",", head = TRUE, stringsAsFactors = FALSE)
class(rta2$time)

Data Export

  • Use write.table() to write data to a CSV file
write.csv(data, file = "temp.csv", row.names = FALSE) 
  • Writing out plots
pdf('myplot.pdf', width = 7, height = 7) # call pdf() before calling plot()
x <- rnorm(10); y <- rnorm(10)
plot(x, y)
dev.off()

Subsseting

Operators that can be used to extract subsets of R objects.

  • '[' and ']' always returns an object of the same class as the original; can be used to select more than one element.
  • '[[' and ']]' is used to extract elements of a list or a data frame; it can only be used to extract a single element.
  • $ is used to extract elements of a list or data frame by name.

Subsetting (cont.)

x <- c("a", "b", "c", "c", "d", "a")
x[1]
x[1:4]
x[x > "a"] 
u <- x > "a" # what's u here?
u
x[u] # subsetting using a boolean vector
y <- list(foo=x, bar=x[u]) 
y
y[[1]]
y$bar
five_gears <- subset(mtcars, gear == 5) # use of subset function for data frames

Data frame vs matrix

Consider the following:

  • Same types or different types? Numeric or other type?
  • Convenient using $ with col names?
  • Data size too big? (memory efficiency and size)
m = matrix(1:400000, 2, 200000) # esp. for a large number of columns!
d = as.data.frame(m)
object.size(m) # 1600200 bytes
object.size(d) # 22400568 bytes
  • Conversion between data frame and matrix
    • as.data.frame()
    • as.matrix() or data.matrix() # consider coercion

Wrap-up exercise

Basics
1) Try installing a new library (lmtest) from Console
2) Try installing another library (ggplot2) from the Packages tab

Challenges
3) From the dataset, hflights, create a subsample based on delay times greater than 10 hours (600 min) from ArrDelay and name it, hflightsSub.
4) Set all of the extreme delays in ArrDelay (more than 300 minutes) in hflightsSub to NA.
5) Convert the data frame, d, back to a matrix named m1 and compare the size of m and m1

Solutions and discussions (15 min)

1)

2)

3)

4)

5)

Homework (Day 1)

  1. Install git on your computer (if it's not already installed)
  2. Create a Github account (if you don't have it yet)
  3. Create a test repository, hmc-r-bootcamp-2018, from GitHub
  4. Create a R project with version control and use the hmc-r-bootcamp-2018 repository's URL to connect your R project with the GitHub repository
  5. Create a new R markdown file (HMC-R-Bootcamp.Rmd) and try “push” to the repository
  6. Change the “HMC-R-Bootcamp.Rmd” from GitHub
  7. Try “pull” the Rmd file to your local repository
  8. Digest what you've learned from Day 1