Last active
June 16, 2020 15:49
-
-
Save seandavi/236d83b863d31387b1f089fedd82474c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
browseURL('https://seandavi.github.io/ITR/r_intro_mechanics.html') | |
1 + 1 | |
x = 10 | |
x | |
y = 20 | |
y | |
y + x | |
browseURL('https://seandavi.github.io/ITR/vectors.html') | |
# vectors | |
b = 1:10 | |
b | |
# subset | |
b[3] | |
b[7] | |
b[3:7] | |
# data.frame | |
browseURL('https://seandavi.github.io/ITR/dataframes_intro.html') | |
d = data.frame(x=1:10,y=letters[1:10]) | |
d | |
head(d) | |
# subset uses [] square brackets with [row, column] | |
d[1,2] | |
d[2,1] | |
d[1,] | |
d[2:5,] | |
# browseURL('https://seandavi.github.io/ITR/control_looping_functions.html') | |
# make a function | |
add_x_and_y = function(xval, yval) { | |
return(xval + yval) | |
} | |
# Use a function | |
# functions have () after them | |
add_x_and_y(20,100) | |
browseURL('https://seandavi.github.io/ITR/dplyr_intro_msleep.html') | |
library(dplyr) | |
library(purrr) | |
library(ggplot2) | |
browseURL('https://seandavi.github.io/sars2pack/') | |
library(sars2pack) | |
# Work with United States disease stats by state | |
# I know which dataset to use, but if you don't | |
# browseURL('https://seandavi.github.io/sars2pack/articles/datasets.html') | |
help('nytimes_state_data') | |
nyt = nytimes_state_data() | |
head(nyt) | |
table(nyt$subset) | |
table(nyt$state) | |
summary(nyt$date) | |
nyt_ny = nyt %>% dplyr::filter(state=='New York') | |
table(nyt_ny$state) | |
nyt_ny_confirmed = nyt_ny %>% dplyr::filter(subset == 'confirmed') | |
table(nyt_ny_confirmed$subset) | |
help('plot_epicurve') | |
plot_epicurve(nyt_ny_confirmed, date_column = 'date', | |
case_column = 'count') | |
###################### | |
# Challenge 1 | |
###################### | |
# Change the code above to look at a different | |
# state. | |
# Bonus: Change the code above to look at multiple | |
# states. | |
# Have been looking at cumulative cases | |
# How about incidence? | |
head(nyt_ny_confirmed) | |
nyt_ny_incidence = nyt_ny_confirmed %>% | |
add_incidence_column() | |
head(nyt_ny_incidence) | |
plot_epicurve(nyt_ny_incidence, date_column = 'date', | |
case_column = 'inc', log=FALSE) | |
plot_epicurve(nyt_ny_incidence, date_column = 'date', | |
case_column = 'inc', log=TRUE) | |
# Bonus: mark day of week (green is Sunday) | |
plot_epicurve(nyt_ny_incidence %>% | |
mutate(dow = lubridate::wday(date)), | |
date_column = 'date', | |
case_column = 'inc', log=TRUE) + | |
geom_point(aes(col=dow==1)) | |
# When did New York hit its peak in cases? | |
nyt_ny_incidence[which.max(nyt_ny_incidence$inc),] | |
# What are the counts over the last week? | |
nyt_ny_incidence %>% dplyr::filter(date>=Sys.Date()-7) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment