NDL R Tutorial
author: Ryan Mears date: March 24, 2014 transition: linear
Overview
Working with data in R with the RStudio GUI
- Get and Shape data into tidy arrangements
- Analyze and Summarize data with R packages
- Explore and Present with data visualization and publication quality tables and plots
Installing R and RStudio
type: section
R
Install R
RStudio
Install RStudio
Get and Shape Data
type: section
Reading Data and Code
- read.csv()
getwd()
- source()
getwd()
Data and Code in R
-
data types
- vectors
- arrays and matrices
- data frames
- lists
- functions
Create variables
- c()
hc_subjs <- c('sbj101','sbj102','sbj105')
sz_subjs <- c('sbj103','sbj104','sbj106')
- seq()
- seq_len(6)
r seq_len(6)
- seq(from = -2, to = 3, by = 1)
r seq(from = -2, to = 3, by = 1)
- seq_len(6)
- rep()
- rep(c('HC','SZ'), times = 1, each = 3)
r rep(c('HC','SZ'), times = 1, each = 3)
- rep(c('HC','SZ'), times = 3, each = 1)
r rep(c('HC','SZ'), times = 3, each = 1)
- rep(c('HC','SZ'), times = 1, each = 3)
Create variables
x <- seq_len(6)
subjs <- c(hc_subjs,sz_subjs)
cbind(x,subjs)
Create variables
- cbind()
hc_2 <- cbind(hc_subjs,"hc")
sz_2 <- cbind(sz_subjs,"sz")
sz_2
- rbind()
grp_subjs <- rbind(hc_2,sz_2)
colnames(grp_subjs)<-c("subj","group")
grp_subjs
Examine variables
- print()
print(hc_subjs)
- length()
length(hc_subjs)
- dim()
dim(grp_subjs)
- str()
str(grp_subjs)
Index and Select
- Indices
grp_subjs[1,1]
grp_subjs[1,]
- Booleans
grp_subjs[,2]=='hc'
Index and Select
- grep(), grepl(), gsub(), substr()
Convert and Combine
- paste(), paste0(), cat()
- join()
- expand.grid()
Subset and Reshape
- filter()
- select()
- reshape()
- melt()
- cast()
- **ply()
Analyze and Summarize
type: section
Compute Parameters
- mutate()
Compare Means
- ttest()
ANOVA
- ezANOVA()
plot(cars)
Explore and Present
type: section
Plot
- plot()
- ggplot()
- ggvis()
Present Results for Publication
- stargazer()
Dynamic Documents
- knit()
Resources
type: subsection
- Papers
- Cheat Sheets
- Wikis
- Webpages
- Books