Jeho Park
May 29, 2018
HMC R Bootcamp 2018
Some materials are adapted from the following websites:
To prepare you to use R to handle and analyze sample data of your choice.
By the end of this two-day bootcamp, you will be able to:
IEEE Spectrum: Popular Programming Languages in 2017
Take a look at RStudio panes and tabs
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)
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"
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)
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!
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
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 has five basic or “atomic” classes of objects:
A vector contains a set of data in any one of the atomic classes.
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)
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
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
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)
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)
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
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.
Take a break!
1)
2)
3)
4)
5)
cpds <- read.csv(file.path('.', 'data', 'cpds.csv'))
head(cpds) # good to look at a few lines
class(cpds) # data.frame
data <- read.table("http://lib.stat.cmu.edu/jcgs/tu", skip=4, header=T)
tail(data)
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)
# save as a CSV file
write.csv(data, file = "temp.csv", row.names = FALSE)
# save as a R data image
save.image(file="myenv.RData")
pdf('myplot.pdf', width = 7, height = 7) # call pdf() before calling plot()
x <- rnorm(10); y <- rnorm(10)
plot(x, y)
dev.off()
Operators that can be used to extract subsets of R objects.
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
Consider the following:
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
attach(mtcars) # Attach mtcars to search path
plot(wt, mpg) # notice objects are called by their names, not mtcars$wt
plot(wt, mpg,
main = "Regression of MPG on Weight",
xlab = "Weight",
ylab = "MPG")
plot(wt, mpg, ann = FALSE)
abline(h=25) # a reference line
abline(lm(mpg~wt)) # look at the argument, what's lm?
title(main = "Regression of MPG on Weight", xlab = "Weight", ylab = "MPG")
par() # view current settings
orig_par <- par() # save current settings
par(col.lab="red") # red x and y labels
plot(wt, mpg) # create a plot with these new settings
par(orig_par) # restore original settings
plot(wt, mpg)
plot(wt, mpg, col.lab="red") # change settings withing plot()
?par # see all the options
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) Using ggplot2, plot a histogram of the flight delays with negative delays set to zero, censoring delay times at a maximum of 60 minutes.
1)
2)
3)
4)
5)