The document provides instructions for an introductory R session, including downloading materials from a GitHub repository and opening an R project file. It outlines logging in, downloading an R project folder containing intro materials, and opening the project file in RStudio.
1. Welcome to an R intro!
1. Log in
2. Go to github.com/sjfox/2016_fall_intro_r,
and download the materials
3. Open up the 2016_fall_intro_r.Rproj in RStudio
20. Data Get data into R Analyze/calcu-
late data
Generate beautiful
figures
Share your results
Data analysis in the “tidyverse”
Slide created by
Sean Leonard
21. Data Get data into R Analyze/calcu-
late data
Generate beautiful
figures
Share your results
Data analysis in the “tidyverse”
Slide created by
Sean Leonard
22. Data Get data into R Analyze/calcu-
late data
Generate beautiful
figures
Share your results
Data analysis in the “tidyverse”
Slide created by
Sean Leonard
23. Data Get data into R Analyze/calcu-
late data
Generate beautiful
figures
Share your results
Data analysis in the “tidyverse”
Slide created by
Sean Leonard
24. Data Get data into R Analyze/calcu-
late data
Generate beautiful
figures
Share your results
Data analysis in the “tidyverse”
Slide created by
Sean Leonard
30. 1st Programming Exercise
1. open up the 2016_fall_intro_r.Rproj
2. Navigate to the code folder and open up
r_intro.Rmd
3. Start playing with code
1. Do: Ask Questions, run code, change things
and see what happens
How to run code:
1. Move cursor to a linecoding block
2. Highlight line(s) of code
3. Type, ctrl+enter (windows) or cmd+enter (mac)
4. See code running in console
5. View output/figures
31. Now you can run code in R, so just need
ingredients for your recipe
32. Now you can run code in R, so just need
ingredients for your recipe
Vectors
33. Now you can run code in R, so just need
ingredients for your recipe
Vectors
Data frames
34. Now you can run code in R, so just need
ingredients for your recipe
Vectors
Data frames
Functions
35. Now you can run code in R, so just need
ingredients for your recipe
Vectors
Data frames
Functions
Stop me if you see anything
on this screen that doesn’t
make sense!
59. Expression
Comparison between left and
right side
== Equality
!= Inequality
< Less than
> Greater than
<= Less than or equal to
>= greater than or equal to
65. select syntax Description
select(col1:colx) All columns between col1 and colx
select(1:x) Columns 1 through x
select(col1, col2) All columns listed
select(-col1) All columns except col1
select(col1:col10, -col3)
All columns between col1 and
col10 except for col3
71. mutate(): add a new column to df
df %>% mutate(new_col_name = expression)
72. mutate(): add a new column to df
df %>% mutate(new_col_name = expression)
73. mutate(): add a new column to df
df %>% mutate(new_col_name = expression)
74. Operation Description
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponentiate
sqrt() Take the square root
log() Take the logarithm (defaults to ln)
exp() Exponentiates (defaults to e^x)
79. group_by(): Make implicit groupings
summarise(): compute summary of groups
How would the code change if you wanted to
find the average gdp for each country instead?
80. Summary Fxn Description
mean() Mean of values
sum() Sum values
median() Median
sd() Standard deviation
var() Variance
cor() Correlation
87. The grammar of graphics (ggplot)
1. Data
•Raw data for plotting
88. The grammar of graphics (ggplot)
1. Data
•Raw data for plotting
2. Geometries
•The shape that will represent the data
•point, line, bar, etc.
89. The grammar of graphics (ggplot)
1. Data
•Raw data for plotting
2. Geometries
•The shape that will represent the data
•point, line, bar, etc.
3. Aesthetics
•axis, color, size, shape, etc.
90. The grammar of graphics (ggplot)
1. Data
•Raw data for plotting
2. Geometries
•The shape that will represent the data
•point, line, bar, etc.
3. Aesthetics
•axis, color, size, shape, etc.
4. Scales
•Mapping data to aesthetic (how to color geoms,
data range to plot, etc)
105. Principles of “tidy” data
1. Every variable forms a column
2. Each observation forms a row
106. Principles of “tidy” data
1. Every variable forms a column
2. Each observation forms a row
Patient Age Height Weight
Jack 30 72 180
Jill 28 64 115
Mary 27 62 112
Messy / Wide
107. Principles of “tidy” data
1. Every variable forms a column
2. Each observation forms a row
Patient Age Height Weight
Jack 30 72 180
Jill 28 64 115
Mary 27 62 112
Patient Characteristic Value
Jack Age 30
Jack Height 72
Jack Weight 180
Jill Age 28
Jill Height 64
Jill Weight 115
Mary Age 27
Mary Height 62
Mary Weight 112
Messy / Wide
Tidy / Long
108. Principles of “tidy” data
1. Every variable forms a column
2. Each observation forms a row
Patient Age Height Weight
Jack 30 72 180
Jill 28 64 115
Mary 27 62 112
Patient Characteristic Value
Jack Age 30
Jack Height 72
Jack Weight 180
Jill Age 28
Jill Height 64
Jill Weight 115
Mary Age 27
Mary Height 62
Mary Weight 112
Messy / Wide
Tidy / Long
109. Principles of “tidy” data
1. Every variable forms a column
2. Each observation forms a row
110. Principles of “tidy” data
1. Every variable forms a column
2. Each observation forms a row
122. R resources
• stack overflow (google)
• Hadley Wickham’s website - http://hadley.nz/
• http://www.r-bloggers.com/how-to-learn-r-2/
• A Beginner's Guide to R (Use R!) by Alain Zuur,
Elena N. Ieno, and Erik Misters
• The Art of R Programming: A Tour of Statistical
Software Design by Norman Matloff
• ggplot2: Elegant Graphics for Data Analysis (Use R!)
by Hadley Wickham. — Maybe wait for the second
edition (it’s slightly outdated)