-
Notifications
You must be signed in to change notification settings - Fork 2
/
Slicing-Dicing Data 10-14-2020.Rmd
745 lines (553 loc) · 18.4 KB
/
Slicing-Dicing Data 10-14-2020.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
---
title: "Slicing and Dicing Covid Data"
author: "Rob Wells"
date: "November 1, 2020"
output: html_document
---
#Load Software
```{r include=FALSE}
library(tidyverse)
#install.packages("rio")
```
#rio handles more than two dozen formats including tab-separated data (with the extension .tsv),
#JSON, Stata, and fixed-width format data (.fwf).
```{r}
hospital <- rio::import('https://raw.githubusercontent.com/Arkansascovid/Main/master/hospital_master.csv')
head(hospital)
```
# Number columns
```{r}
ncol(hospital)
```
# Number rows
```{r}
nrow(hospital)
```
- **Clean Names**
```{r}
hospital <- janitor::clean_names(hospital)
```
##Check it out - Look at the data types.
```{r}
#chr stands for character vectors, or strings.
#int stands for integers.
#dbl stands for doubles, or real numbers.
#dttm stands for date-times (a date + a time).
glimpse(hospital)
```
- **Cleaning: Convert data from List variables to numeric**
```{r include=FALSE}
#text to date
hospital$date <- as.Date(hospital$date)
#text to numeric
hospital[3:6] <- lapply((hospital)[3:6], as.numeric)
```
```{r}
#Eliminate junk first column
hospital <- hospital [ -c(1) ]
```
```{r}
#Rename columns
#colnames(hospital)[2:5] <- c("Hospitalized", "Vent", "Ever_Hospitalized", "Ever_on_a_Vent")
```
#----------------------------------------------------------------------------------#
#Filtering and Selecting Columns
#----------------------------------------------------------------------------------#
#Create a table sorted by number of hospitalized
```{r include=FALSE}
#Always a good idea to rename the target as something different in case you mess up something
#this is hospital1
hospital1 <- hospital %>%
arrange(desc(hospitalized))
```
```{r}
#Check out the new table, all sorted cool
head(hospital1)
```
#YOUR TURN: Create a Table sorted by vent
```{r}
vent <- hospital %>%
arrange(desc(vent))
head(vent)
```
#Filtering and Selecting Columns
```{r}
#Filter just the October entries
hospital1 <- hospital %>%
filter(date > "2020-09-30")
(hospital1)
```
#Fix the dates for chronological order
```{r}
#Fix the dates for chronological order
hospital1 <- hospital1 %>%
arrange(desc(date))
(hospital1)
```
#YOUR TURN: Create a Table just with the August entries
#HINT: https://stackoverflow.com/questions/22420487/subsetting-data-table-set-by-date-range-in-r
#https://rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf
testset[date>="2013-08-02" & date<="2013-11-01"]
```{r}
hospital1 <- hospital %>%
filter(date >= "2020-08-01" & date <= "2020-08-31")
hospital1
```
#dplyr
#We are using basic commands in the dplyr package
#https://dplyr.tidyverse.org/
#Select just three columns
```{r}
vent2 <- hospital %>%
select(date, vent, ever_on_a_vent)
head(vent2)
write.csv(vent2, "vent2.csv")
```
#YOUR TURN
#IMPORT the master file from the Arkansascovid GitHub site
#1) Create a new filtered table with October only files
#2) Create a new table just with deaths, but for all dates
#3) Create a new table with all of the testing data
#Bonus: Take tables 1, 2 & 3 and build Flourish Charts with them
#IF YOU FINISH THAT, REVEL IN YOUR NERD POWERS
#IMPORT the master file from the Arkansascovid GitHub site
```{r}
master <- rio::import("https://raw.githubusercontent.com/Arkansascovid/Main/master/master_file.csv")
master$mydate <- as.Date(master$mydate)
head(master)
```
#2) Create a new table just with deaths, but for all dates
#Build Confirmed Table
```{r}
deaths <- master %>%
select(mydate, deaths, New_Deaths_Today, confirmed_death, probable_death, Pct_Deaths_vs_Recoveries, Pct_Recoveries_vs_Deaths )
head(deaths)
```
#2a) Using advanced dplyr command
```{r}
#Use contains to search by string to find the variables by name
#this grabs everything except date
x <- select(master, contains("death"))
names(x)
```
```{r}
#copy those names into a select sequence
death2 <- master %>%
select("mydate", "deaths", "confirmed_death", "probable_death", "New_Deaths_Today", "Pct_Deaths_vs_Recoveries", "Pct_Recoveries_vs_Deaths" )
```
#3) Create a new table with all of the testing data
```{r}
test <- master %>%
select("mydate", "total_tests", "pcr_test", "antigen_test", "pcr_pos_test", "pcr_neg_test", "antigen_pos_test", "antigen_neg_test", "total_pos_test", "total_neg_test", "New_Tests_Dashboard", "Tested_Population" )
```
#3a) Using advanced dplyr command
```{r}
#Use contains to search by string to find the variables by name
#this grabs everything except date
x <- select(master, contains("test"))
names(x)
```
```{r}
#copy those names into a select sequence
test <- master %>%
select("mydate", "total_tests", "pcr_test", "antigen_test", "pcr_pos_test", "pcr_neg_test", "antigen_pos_test",
"antigen_neg_test", "total_pos_test", "total_neg_test", "New_Tests_Dashboard", "Tested_Population" )
```
#----------------------------------------------------------------------------------#
# Math
#----------------------------------------------------------------------------------#
###Summary Statistics
Here is a quick way to view the range of your data
```{R}
summary(master$death)
```
```{r}
#Crime$Murder.Robbery <- Crime$Murder + Crime$Robbery
death2$NEWtotal <- death2$confirmed_death + death2$probable_death
head(death2)
```
```{r}
#percentage of a column
#calculate confirmed as percentage of total deaths
death2$pct_confirmed <- (death2$confirmed_death / death2$deaths)*100
head(death2)
glimpse(master)
```
#YOUR TURN
#1) Antigen positivity rate
#2) Antigen positive rate, just for state of Arkansas
#3) Percent positive rate overall (positive / total tests)
#4) Percent positive rate overall, just for state of Arkansas
#5) Percent positive rate, just Arkansas, highest to lowest by date
#6) Create a heatmap with results from #5
#----------------------------------------------------------------------#
#Student Feedback
#----------------------------------------------------------------------#
#1) Antigen positivity rate
```{r}
#Error - you overwrote your data with this calculation. Need to create a new column
#master2$antigen_pos_test <- (master2$antigen_pos_test / master2$antigen_test)*100
master$antigen_pct <- (master$antigen_pos_test / master$antigen_test)*100
#Wells edit - create mini table to look at results
antigen <- master %>%
select(county_nam, mydate, antigen_pos_test, antigen_test, antigen_pct)
head(antigen)
```
#1 Antigen positivity rate
```{r}
#Error - divided antigen into the wrong denominator: should be antigen_test. total_pos_test is all PCR and antigen
# master2 <- master %>% mutate(Antigen_positivity_rate = (antigen_test / total_pos_test)*100)
# print(master2)
#However: GOOD to use mutate and the dplyr syntax
#Wells Edit.
#Fixed Math
#Create a mini table to check your math
master <- master %>%
mutate(Antigen_positivity_rate1 = (antigen_test / total_pos_test)*100) %>%
mutate(Antigen_positivity_rate2 = (antigen_pos_test / antigen_test)*100)
test2 <- master %>%
select(county_nam, mydate, antigen_pos_test, antigen_test, total_pos_test, Antigen_positivity_rate1, Antigen_positivity_rate2)
head(test2)
```
#Wells Edit
#Good to see you find a package, writexl.
#write.csv is base R and does a similar thing.
```{r}
library(writexl)
write_xlsx(master2,"/Users/obedlamy/Desktop/Data Journalism/Antigen.xlsx")
```
#3) Percent positive rate overall (positive / total tests)
```{r}
#Wells edit: I would not create another table master3. Just do it to master and the create a mini table to view results
# master3 <- master %>% mutate(Percent_positive = (positive / total_tests)*100)
# print(master3)
#Wells edit
#I would be very specific on the type of test and divide an apple into an apple.
#total_tests is problematic since positive and negative do not add up to total_tests
#instead divide pcr_pos into pcr_test
master <- master %>%
#drop the next line down to better keep track of things
mutate(PCR_Percent_positive = (pcr_pos_test / pcr_test)*100) %>%
mutate(Percent_positive = (positive / total_tests)*100)
test4 <- master %>%
select(county_nam, mydate, Percent_positive, PCR_Percent_positive, positive, total_tests, pcr_pos_test, pcr_test )
head(test4)
```
#5) Percent positive rate, just Arkansas, highest to lowest by date
```{r}
test <- master %>%
select(county_nam, mydate, PCR_Percent_positive) %>%
filter(county_nam == "Arkansas_all_counties") %>%
#arrange(desc(PCR_Percent_positive))
arrange(desc(mydate))
test
```
#----------------------------------------------------------------------------------#
# Lubridate Introduction
# Lubridate vignette
```{r}
browseVignettes("lubridate")
```
#Introduction to Lubridate
#Tutorials Based on Andrew Ba Tran dealing-with-dates.R
#Install lubridate if you haven't already
```{r}
#install.packages("lubridate")
#library(lubridate)
```
#Load some data
```{r}
some_date <- "12-31-1999"
```
```{r}
# NOTE: IF YOU GET AN ERROR ABOUT NOT HAVING A PACKAGE CALLED stringi
# UNCOMMENT AND RUN THE LINES BELOW IF YOU HAVE A MAC MACHINE
#install.packages("glue", type="mac.binary")
#install.packages("stringi", type="mac.binary")
#install.packages("stringr", type="mac.binary")
#install.packages("lubridate", type="mac.binary")
```
#Ok, here we go
```{r}
library(lubridate)
mdy(some_date)
```
```{r}
data <- data.frame(First=c("Charlie", "Lucy", "Peppermint"),
Last=c("Brown", "van Pelt", "Patty"),
birthday=c("10-31-06", "2/4/2007", "June 1, 2005"))
data$DOB <- mdy(data$birthday)
data
```
```{r}
data$year <- year(data$DOB)
data$month <- month(data$DOB, label=TRUE)
data$week <- week(data$DOB)
data$day <- day(data$DOB)
data$weekday <- wday(data$DOB, label=TRUE, abbr=FALSE)
data
```
#------------------------------------------------------#
#Now, apply this to the Covid data
#------------------------------------------------------#
#Load the Covid master_file
#IMPORT the master file from the Arkansascovid GitHub site
```{r}
master <- rio::import("https://raw.githubusercontent.com/Arkansascovid/Main/master/master_file.csv")
str(master)
```
#Question 1: Convert date to mdy -- see line 376
```{r}
master$date <- ymd(master$mydate)
#str(master)
```
#Question 2: Mini table for Master to just county_nam, date, positive, Pct_Positive_Cumulative
```{r}
test5 <- master %>%
select(county_nam, date, positive, Pct_Positive_Cumulative)
head(test5)
```
#Create Separate Year Field
```{r}
test5$year <- year(test5$date)
test5
```
#Create Separate Month Column
```{r}
test5$month <- month(test5$date, label=TRUE)
test5
```
#Create Separate Week Column
```{r}
test5$week <- week(test5$date)
test5
```
#Create Separate Day Column
```{r}
test5$day <- day(test5$date)
test5
```
#Create Separate Weekday Column
```{r}
test5$weekday <- wday(test5$date, label=TRUE, abbr=FALSE)
test5
```
#Using master_file, answer a recent reader's question:
#Was this a record number of cases for a Saturday?
```{r}
#format dates
library(lubridate)
master$date <- ymd(master$mydate)
master$year <- year(master$date)
master$month <- month(master$date, label=TRUE)
master$week <- week(master$date)
master$day <- day(master$date)
master$weekday <- wday(master$date, label=TRUE, abbr=FALSE)
#Mini table
mini <- master %>%
select(county_nam, date, year, month, week, day, weekday)
mini
```
#--------------------------------------------------------------------------------#
#Weekly Change in Cases Calculations
#--------------------------------------------------------------------------------#
#Set Dates
```{r}
#today's date
today <- Sys.Date()
#NOTE: IF YOU ARE RUNNING THIS A DAY LATE, USE THIS CODE TO WORK PROPERLY
#today <- Sys.Date()-1
#today_county$mydate <-"2020-09-22- THE OLD DATE...."
#yesterday's date
yesterday <- (today-1)
```
```{r}
df1 <- master %>%
filter(County_Name=="Arkansas_all_counties")
df1 <- df1 %>%
mutate(DATE = ymd(Date))
```
#date formatting
```{r}
df1$week <- isoweek(df1$DATE)
df1$month <- month(df1$DATE)
```
#calculations by week
```{r}
newcases <- df1 %>%
group_by(week) %>%
summarise(week_newcases = sum(New_Cases_Today, na.rm = TRUE))
```
#import table to name the weeks beginning Monday
```{r}
weeks_numbers <- rio::import("https://raw.githubusercontent.com/profrobwells/CovidFall2020/master/weeks_numbers.csv")
weeks_numbers$Date <- lubridate::mdy(weeks_numbers$Date)
```
#Join with cases; manually change date filter after a week
```{r}
weeklycases <- newcases %>%
right_join(weeks_numbers, by=c("week"="Number")) %>%
filter(Date<today) %>%
distinct() %>%
arrange(desc(Date))
weeklycases <- weeklycases %>%
rename(Weekly_Total_New_Cases = week_newcases, Week_Beginning = Date)
#write.csv(weeklycases, "weeklycases.csv")
```
# Group_By
#Takes existing data and groups specific variables together for future operations. Many operations are performed on groups.
```{r}
mini2 <- mini %>%
filter(county_nam =="Arkansas_all_counties") %>%
group_by(month)
mini2
```
#----------------------------------------------------------------------------------#
# Make a List of Top 10, or Top 5 or Top anything
#----------------------------------------------------------------------------------#
#top_n
#https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/top_n
#Top 10 days with new deaths
```{r}
top_deaths <- master %>%
select(county_nam, mydate, New_Deaths_Today) %>%
filter(county_nam == "Arkansas_all_counties") %>%
top_n(10, New_Deaths_Today) %>%
arrange(desc(New_Deaths_Today))
top_deaths
```
#Table with the top five counties by deaths for November
```{r}
#Mini table with
novdeath <- master %>%
select(month, county_nam, New_Deaths_Today) %>%
filter(month =="Nov") %>%
filter(!(county_nam == "Arkansas_all_counties")) %>%
group_by(county_nam, month) %>%
summarise(Nov_deaths = sum(New_Deaths_Today, na.rm = TRUE))
novdeath
```
```{r}
novdeath1 <- top_n(ungroup(novdeath), 5, Nov_deaths)
novdeath1 <- novdeath1 %>%
arrange(desc(Nov_deaths))
novdeath1
```
#---------------------------------------------------------
#Resume Here Monday
#---------------------------------------------------------
#Using master_file, answer record number of cases for a Monday
#Bring in Master file, format dates
```{r}
master <- rio::import("https://raw.githubusercontent.com/Arkansascovid/Main/master/master_file.csv")
#format dates
library(lubridate)
master$date <- ymd(master$mydate)
master$year <- year(master$date)
master$month <- month(master$date, label=TRUE)
master$week <- week(master$date)
master$day <- day(master$date)
master$weekday <- wday(master$date, label=TRUE, abbr=FALSE)
#Mini table
mini <- master %>%
select(county_nam, date, year, month, week, day, weekday)
mini
```
#Create Monday-only table, statewide data, call is "mon"
```{r}
mon <- master
```
#Make a Basic Chart
```{r}
ggplot(mon, aes(x=date, y=New_Cases_Today, color = New_Cases_Today, fill=New_Cases_Today)) +
geom_col() +
theme(axis.text.x = element_text(angle = 45, vjust = 1.2, hjust = 2.1)) +
labs(y="Cases Per Day", title="Record COVID-19 Cases on Wednesdays in Arkansas", caption="Source: ADH")+
theme(legend.position="none")
```
#Improve on Chart, flipped and with labels
```{r}
mon %>%
filter(date > "2020-03-26") %>%
filter(New_Cases_Today > 500) %>%
ggplot(aes(x = reorder(date, New_Cases_Today),
y = New_Cases_Today,
fill = New_Cases_Today)) +
geom_col(position = "dodge", show.legend = FALSE) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
#label formatting. Scales, into percentages. hjust moves to the grid
geom_text(aes(label = (New_Cases_Today)), position = position_stack(vjust = 1), hjust = -5., size = 2.5) +
#format the x axis. sets the grid to maximum 30%
scale_y_continuous(limits=c(0, 1800)) +
coord_flip() +
labs(title = "Record COVID-19 Cases on Wednesdays in Arkansas",
subtitle = "Cases 500 and Greater",
caption = "Graphic by Rob Wells, 11-11-2020",
y="Source: Arkansas Dept of Health",
x="")
ggsave("Wednesday top cases 11_11.png", device = "png", width=10,height=8, dpi=800)
```
#Or Use top_n
```{r}
mon %>%
top_n(10, New_Cases_Today) %>%
ggplot(aes(x = reorder(date, New_Cases_Today),
y = New_Cases_Today,
fill = New_Cases_Today)) +
geom_col(position = "dodge", show.legend = FALSE) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
#label formatting. Scales, into percentages. hjust moves to the grid
geom_text(aes(label = (New_Cases_Today)), position = position_stack(vjust = 1), hjust = -5., size = 2.5) +
#format the x axis. sets the grid to maximum 30%
scale_y_continuous(limits=c(0, 1800)) +
coord_flip() +
labs(title = "Record COVID-19 Cases on Wednesdays in Arkansas",
subtitle = "Cases 500 and Greater",
caption = "Graphic by Rob Wells, 11-11-2020",
y="Source: Arkansas Dept of Health",
x="")
ggsave("Wednesday top cases 11_11.png", device = "png", width=10,height=8, dpi=800)
```
#Reader Question: Number of Deaths since Oct 8
```{r}
```
#Using master_file, develop monthly totals and percentage changes for new cases and deaths
```{r}
```
#Percentage change by month new cases
```{r}
```
#Percentage change by month new deaths
```{r}
```
#White House Report Question
# Arkansas had 193 new cases per 100,000 population in the last week, compared to a national average of 90 per 100,000.
#CALCULATE total new cases by week, divide into our population
```{r}
```
#Your Turn
#Top 5 Deaths on Saturdays: A table just with date, New Deaths
```{r}
```
#Make a chart
```{r}
```
#----------------------------------------------------------------------------------#
## NOTES: HOW TO Retrieve Data from Your Hard Drive
#----------------------------------------------------------------------------------#
- **Download Daily Hospital Data - Manual Input" from Google Sheet**
https://docs.google.com/spreadsheets/d/1ikblX8tikM59ma1AftkqgGbyeZkXB6DuBtwMsVeoGYw/edit#gid=701586163
- **Get the File Path on Your Hard Drive**
1) Find the File and LEFT CLICK on it (or Control + Click).
A menu appears.
2) Continue holding down CONTROL.
3) Now Hold down CONTROL + OPTION at same time.
Copy "Daily State Data - Manual Input" as Path.
It will look like this:
/Users/rswells/Downloads/Daily State Data - Manual Input.xlsx
4) Paste that path below
```{r}
#An example of downloading data from your hard drive
#hospital <- rio::import("/Users/rswells/Downloads/Daily State Data - Manual Input.xlsx")
#This loads in "Daily State Data - Manual Input" from Google Sheet
```