Let's start with...
The basics
Non-credit
6 weeks
Watch the videos and do the exercises on your own (or with friends/classmates), come together for lab
Practice by yourself in between classes
Everything you need is at http://intro-to-r-2020.louisahsmith.com
You are not going to break anything!
Rising 5th-year PhD candidate in Epidemiology
Started using R during my master's (so 6 years of experience); learned mostly by doing
Problem sets, manuscripts, slides, website all in R
Almost 100 R projects on my computer, over 1000 R scripts
Rising 5th-year PhD candidate in Epidemiology
Started using R during my master's (so 6 years of experience); learned mostly by doing
Problem sets, manuscripts, slides, website all in R
Almost 100 R projects on my computer, over 1000 R scripts
I have to Google things literally every time I use R!
An integrated development environment is software that makes coding easier
Setup...
1
Your turn...
mean()
, lm()
, table()
, etc.base
, stats
, graphics
, etc.ggplot2
, dplyr
, data.table
, survival
, etc.Image from Zhi Yang
install.packages("survival")
library(survival)
"You only have to buy the book once, but you have to go get it out of the bookshelf every time you want to read it."
install.packages("survival")library(survival)survfit(...)
Several days later...
library(survival)coxph(...)
Demonstration...
install.packages
, packages are downloaded from CRAN (The Comprehensive R Archive Network)tidyverse
install.packages(tidyverse)
actually downloads more than a dozen packages*library(tidyverse)
loads:
ggplot2
, dplyr
, tidyr
, readr
, purrr
, tibble
, stringr
, forcats
*See which ones at https://tidyverse.tidyverse.org
2
Your turn...
tidyverse
"package"tidyverse
packagesmy-project/ ├─ my-project.Rproj ├─ README ├─ data/ │ ├── raw/ │ └── processed/ ├─ code/ ├─ results/ │ ├── tables/ │ ├── figures/ │ └── output/ └─ docs/
An .Rproj
file is mostly just a placeholder. It remembers various options, and makes it easy to open a new RStudio session that starts up in the correct working directory. You never need to edit it directly.
A README file can just be a text file that includes notes for yourself or future users.
I like to have a folder for raw data -- which I never touch -- and a folder(s) for datasets that I create along the way.
R-course/ ├─ 01-week/ │ ├── 01-week.Rproj │ ├── 01-exercises.R │ ├── 01-lab.Rmd │ ├── 01-slides.pdf │ └── data/ │ └── nlsy.csv ├─ 02-week/ │ ├── 02-week.Rproj │ ├── 02-exercises.R │ ├── 02-lab.Rmd │ ├── 02-slides.pdf │ └── data/ │ └── nhanes.xlsx ├── 03-week/
.Rproj
fileDemonstration...
<-
for assignmentCreate an object vals
that contains and sequence of numbers:
# create valuesvals <- c(1, 645, 329)
Put your cursor at the end of the line and hit ctrl/cmd + enter.
Now vals
holds those values.
We can see them again by running just the name (put your cursor after the name and press ctrl/cmd + enter again).
vals
## [1] 1 645 329
No assignment arrow means that the object will be printed to the console.
We could also create a character vector:
chars <- c("dog", "cat", "rhino")chars
## [1] "dog" "cat" "rhino"
Or a logical vector:
logs <- c(TRUE, FALSE, FALSE)logs
## [1] TRUE FALSE FALSE
We'll see more options as we go along!
We created vectors with the c()
function (c
stands for concatenate)
We could also create a matrix of values with the matrix()
function:
# turn the vector of numbers into a 2-row matrixmat <- matrix(c(234, 7456, 12, 654, 183, 753), nrow = 2)mat
## [,1] [,2] [,3]## [1,] 234 12 183## [2,] 7456 654 753
The numbers in square brackets are indices, which we can use to pull out values:
# extract second rowmat[2, ]
## [1] 7456 654 753
We usually do analysis in R with dataframes (or some variant).
Dataframes are basically like spreadsheets: columns are variables, and rows are observations.
gss_cat
## # A tibble: 21,483 x 9## year marital age race rincome partyid relig denom tvhours## <int> <fct> <int> <fct> <fct> <fct> <fct> <fct> <int>## 1 2000 Never marr… 26 White $8000 to 99… Ind,near rep Protestant Southern ba… 12## 2 2000 Divorced 48 White $8000 to 99… Not str repu… Protestant Baptist-dk … NA## 3 2000 Widowed 67 White Not applica… Independent Protestant No denomina… 2## 4 2000 Never marr… 39 White Not applica… Ind,near rep Orthodox-ch… Not applica… 4## 5 2000 Divorced 25 White Not applica… Not str demo… None Not applica… 1## 6 2000 Married 25 White $20000 - 24… Strong democ… Protestant Southern ba… NA## 7 2000 Never marr… 36 White $25000 or m… Not str repu… Christian Not applica… 3## 8 2000 Divorced 44 White $7000 to 79… Ind,near dem Protestant Lutheran-mo… NA## 9 2000 Married 44 White $25000 or m… Not str demo… Protestant Other 0## 10 2000 Married 47 White $25000 or m… Strong repub… Protestant Southern ba… 3## # … with 21,473 more rows
as_tibble(gss_cat)[, 1:4]
# A tibble: 21,483 x 4 year marital age race <int> <fct> <int> <fct> 1 2000 Never married 26 White 2 2000 Divorced 48 White 3 2000 Widowed 67 White 4 2000 Never married 39 White 5 2000 Divorced 25 White 6 2000 Married 25 White 7 2000 Never married 36 White 8 2000 Divorced 44 White 9 2000 Married 44 White10 2000 Married 47 White# … with 21,473 more rows
as.data.frame(gss_cat)[, 1:4]
year marital age race1 2000 Never married 26 White2 2000 Divorced 48 White3 2000 Widowed 67 White4 2000 Never married 39 White5 2000 Divorced 25 White6 2000 Married 25 White7 2000 Never married 36 White8 2000 Divorced 44 White9 2000 Married 44 White10 2000 Married 47 White11 2000 Married 53 White12 2000 Married 52 White13 2000 Married 52 White14 2000 Married 51 White15 2000 Divorced 52 White16 2000 Married 40 Black17 2000 Widowed 77 White18 2000 Never married 44 White19 2000 Married 40 White20 2000 Married 45 Black
dat1 <- tibble( age = c(24, 76, 38), height_in = c(70, 64, 68), height_cm = height_in * 2.54)dat1
## # A tibble: 3 x 3## age height_in height_cm## <dbl> <dbl> <dbl>## 1 24 70 178.## 2 76 64 163.## 3 38 68 173.
dat2 <- tribble( ~n, ~food, ~animal, 39, "banana", "monkey", 21, "milk", "cat", 18, "bone", "dog")dat2
## # A tibble: 3 x 3## n food animal## <dbl> <chr> <chr> ## 1 39 banana monkey## 2 21 milk cat ## 3 18 bone dog
4
Your turn...
01-week/01-todo.R
Let's start with...
The basics
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |