-
Notifications
You must be signed in to change notification settings - Fork 0
/
appendix.rmd
1507 lines (1164 loc) · 54.4 KB
/
appendix.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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Additional Code Not Shown in Final Report"
---
```{r setup, message=FALSE, warning=FALSE, include=FALSE}
library(tint)
# invalidate cache when the tufte version changes
knitr::opts_chunk$set(
cache.extra = packageVersion("tint"),
include = FALSE,
tidy = TRUE
)
options(htmltools.dir.version = FALSE)
```
Loading libraries:
```
library(readr)
library(plyr)
library(tidyr)
library(lubridate)
library(tidyverse)
library(dplyr)
library(ggplot2)
```
#Cleaning NMSS Data
## Konrad & Ramya: Cleaning donations data for 2013-2017:
Reading in the data:
```
donations2013 <- read_csv("C:\\Users\\Konrad\\Desktop\\ETM 527\\Donations\\2013 Bike Donations Date Fixed.csv")
donations2014 <- read_csv("C:\\Users\\Konrad\\Desktop\\ETM 527\\Donations\\2014 Bike Donations Date Fixed.csv")
donations2015 <- read_csv("C:\\Users\\Konrad\\Desktop\\ETM 527\\Donations\\2015 Bike Donations Date Fixed.csv")
donations2016 <- read_csv("C:\\Users\\Konrad\\Desktop\\ETM 527\\Donations\\2016 Bike Donations Date Fixed.csv")
donations2017 <- read_csv("C:\\Users\\Konrad\\Desktop\\ETM 527\\Donations\\2017 Bike Donations Date Fixed.csv")
```
Comparing column names to make sure the data is in the right order and all fields are the same:
```
cols2013 <- colnames(donations2013)
cols2014 <- colnames(donations2014)
cols2015 <- colnames(donations2015)
cols2016 <- colnames(donations2016)
cols2017 <- colnames(donations2017)
identical(cols2013, cols2014)
identical(cols2013, cols2015)
identical(cols2013, cols2016)
identical(cols2013, cols2017)
remove(cols2013, cols2014, cols2015, cols2016, cols2017)
```
Changing some columns to factors:
```
cols <- c(1, 2, 3, 4, 6, 7, 10:19, 21:32, 35, 36, 38, 39, 44:49)
donations2013[,cols] <- lapply(donations2013[,cols], factor)
donations2014[,cols] <- lapply(donations2014[,cols], factor)
donations2015[,cols] <- lapply(donations2015[,cols], factor)
donations2016[,cols] <- lapply(donations2016[,cols], factor)
donations2017[,cols] <- lapply(donations2017[,cols], factor)
```
### Refactoring certain columns:
#### Donor Gender:
```
#summary(donations2013$`Donor Gender`)
levels(donations2013$`Donor Gender`) <- list(Female=c("Female", "F", "Feiale"), Male=c("Male", "M", "Iale"))
#summary(donations2013$`Donor Gender`)
#summary(donations2014$`Donor Gender`)
levels(donations2014$`Donor Gender`) <- list(Female=c("Female", "F", "Feiale"), Male=c("Male"))
#summary(donations2014$`Donor Gender`)
#summary(donations2015$`Donor Gender`)
levels(donations2015$`Donor Gender`) <- list(Female=c("Female", "F", "Femala"), Male=c("Male"))
#summary(donations2015$`Donor Gender`)
#summary(donations2016$`Donor Gender`)
levels(donations2016$`Donor Gender`) <- list(Female=c("Female", "F", "Femala"), Male=c("Male"))
#summary(donations2016$`Donor Gender`)
#summary(donations2017$`Donor Gender`)
levels(donations2017$`Donor Gender`) <- list(Female=c("Female", "F", "Femala", "Famale"), Male=c("Male", "M"))
#summary(donations2017$`Donor Gender`)
```
### Prior participants:
```
#summary(donations2013$`Is Prior Participant`)
levels(donations2013$`Is Prior Participant`) <- list(Yes = c("Yes"))
#summary(donations2013$`Is Prior Participant`)
#summary(donations2014$`Is Prior Participant`)
levels(donations2014$`Is Prior Participant`) <- list(Yes = "Yes")
#summary(donations2014$`Is Prior Participant`)
#summary(donations2015$`Is Prior Participant`)
levels(donations2015$`Is Prior Participant`) <- list(Yes = "Yes")
#summary(donations2015$`Is Prior Participant`)
#summary(donations2016$`Is Prior Participant`)
levels(donations2016$`Is Prior Participant`) <- list(Yes = "Yes")
#summary(donations2016$`Is Prior Participant`)
#summary(donations2017$`Is Prior Participant`)
levels(donations2017$`Is Prior Participant`) <- list(Yes = "Yes")
#summary(donations2017$`Is Prior Participant`)
```
### Is Team Captain
```
#summary(donations2013$`Is Team Captain`)
levels(donations2013$`Is Team Captain`) <- list(Yes ="TRUE", No = "FALSE")
#summary(donations2013$`Is Team Captain`)
#summary(donations2014$`Is Team Captain`)
levels(donations2014$`Is Team Captain`) <- list(Yes = c("TRUE", "PRUE"), No = "FALSE")
#summary(donations2014$`Is Team Captain`)
#summary(donations2015$`Is Team Captain`)
levels(donations2015$`Is Team Captain`) <- list(Yes = "TRUE", No = c("FALSE", "BALSE"))
#summary(donations2015$`Is Team Captain`)
#summary(donations2016$`Is Team Captain`)
levels(donations2016$`Is Team Captain`) <- list(Yes = c("TRUE", "PRUE"), No = "FALSE")
#summary(donations2016$`Is Team Captain`)
#summary(donations2017$`Is Team Captain`)
levels(donations2017$`Is Team Captain`) <- list(Yes = c("TRUE"), No = c("FALSE", "BALSE", "FALSA"))
#summary(donations2017$`Is Team Captain`)
```
### Gift Payment Method
```
#summary(donations2013$`Gift Payment Method`)
levels(donations2013$`Gift Payment Method`) <- list(Cash = "Cash", Check= "Check", `Credit Card` = "Credit Card")
#summary(donations2013$`Gift Payment Method`)
#summary(donations2014$`Gift Payment Method`)
levels(donations2014$`Gift Payment Method`) <- list(Cash = "Cash", Check= "Check", `Credit Card` = "Credit Card")
#summary(donations2014$`Gift Payment Method`)
#summary(donations2015$`Gift Payment Method`)
# No NAs or anything to refactor
#summary(donations2016$`Gift Payment Method`)
levels(donations2016$`Gift Payment Method`) <- list(Cash = "Cash", Check= "Check", `Credit Card` = c("Credit Card", "Credit Car"))
#summary(donations2016$`Gift Payment Method`)
#summary(donations2017$`Gift Payment Method`)
levels(donations2017$`Gift Payment Method`) <- list(Cash = "Cash", Check= "Check", `Credit Card` = c("Credit Card", "Credit Car"))
#summary(donations2017$`Gift Payment Method`)
```
### Gift Type
```
#summary(donations2013$`Gift Type`)
levels(donations2013$`Gift Type`) <- list(Offline=c("offline", "ofbline"), Online="online")
#summary(donations2013$`Gift Type`)
#summary(donations2014$`Gift Type`)
levels(donations2014$`Gift Type`) <- list(Offline="offline", Online=c("online", "ojline"))
#summary(donations2014$`Gift Type`)
#summary(donations2015$`Gift Type`)
levels(donations2015$`Gift Type`) <- list(Offline="offline", Online="online")
#summary(donations2015$`Gift Type`)
#summary(donations2016$`Gift Type`)
levels(donations2016$`Gift Type`) <- list(Offline="offline", Online=c("online", "onhine", "onlina"))
#summary(donations2016$`Gift Type`)
#summary(donations2017$`Gift Type`)
levels(donations2017$`Gift Type`) <- list(Offline="offline", Online=c("online", "ojline", "onlina"))
#summary(donations2017$`Gift Type`)
```
### Offline status
```
summary(donations2013$`Offline Status`)
summary(donations2014$`Offline Status`)
summary(donations2015$`Offline Status`)
summary(donations2016$`Offline Status`)
levels(donations2016$`Offline Status`)<- list(confirmed = c("confirmed", "confirme`"), `Teamraiser Participant Gift` = c("(Teamraiser Participant Gift", "Teamraiser Participant Gift"))
summary(donations2017$`Offline Status`)
```
### Dealing with dates
* Date format was changed in Excel * to eg 08/12/13 to enable easy conversion of dates.
```
#Since each dataset is the same, can identify all date fields from just 2013 data
date_cols <- which(names(donations2013)%in%c("Event Date", "Date Recorded", "Donor Opt-out Date", "Registration Date", "Team Creation Date"))
donations2013[, date_cols] <- lapply(donations2013[, date_cols], as.Date, "%m/%d/%y")
donations2014[, date_cols] <- lapply(donations2014[, date_cols], as.Date, "%m/%d/%y")
donations2015[, date_cols] <- lapply(donations2015[, date_cols], as.Date, "%m/%d/%y")
donations2016[, date_cols] <- lapply(donations2016[, date_cols], as.Date, "%m/%d/%y")
donations2017[, date_cols] <- lapply(donations2017[, date_cols], as.Date, "%m/%d/%y")
```
### Writing out Formatted Donations Data to an RDS file
```
saveRDS(donations2013, "C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2013.rds")
saveRDS(donations2014, "C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2014.rds")
saveRDS(donations2015, "C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2015.rds")
saveRDS(donations2016, "C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2016.rds")
saveRDS(donations2017, "C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2017.rds")
```
## Konrad: Cleaning Bike Teams Data
```
teams <- read_csv("C:\\Users\\Konrad\\Desktop\\ETM 527\\Bike Teams\\2013-2017 Bike Teams Date Format Fixed.csv")
```
```
#summary(teams)
factor_cols <- c(1:5, 7, 8, 10, 11, 20:22, 26)
teams[ ,factor_cols] <- lapply(teams[ ,factor_cols], factor)
# Removed time from team creation date and Event Date (in Excel)!!!
teams$`Team Creation Date` <- as.Date(teams$`Team Creation Date`, format = '%m/%d/%y')
teams$`Event Date` <- as.Date(teams$`Event Date`, format = '%m/%d/%y')
summary(teams$`Team Division`)
levels(teams$`Team Division`) <- list(Association = "Association", Brewery="Beer/Brewery", `Bike Club`="Bike Club", `Bike Shop` = c("Bike Shop", "Bike Shops"), `Civic Team` = "Civic Team", `Club/Organization` = "Club/Organization", Corporate = c("Corporate", "Corporation"), `Family/Friends` = c("Family and Friends", "Family/Friends", "Frien`s and Family", "Friend and Family", "Friends and Family"), Ohana = "Ohana", `Ohana/Friends` = "Ohana and Friends", Open = "Open", `Open Team` = "Open Team", Organization = c("Organization", "Organization (Clubs, Civic Groups, etc.)", "Organization (Clubs; Civic Groups; etc.)", "Organization (Clubs; Civic Groups; Place of Worship; etc.)"), Other = "Other", `Place of Worship` = c("Place of Worship", "Place of worship", "Religious"), School = "School", `Small Business` = "Small Business", `Volunteer Group` = "Volunteer Group")
```
```
#summary(teams$`Event Type`)
#summary(teams$`Internal Event Name`)
#summary(teams$`Event ID`)
#summary(teams$`Team Creation Date`)
#summary(teams$`Captain Email Domain`)
#summary(teams$Company)
#summary(teams$`Team Name`)
#summary(teams$`Number of Participants`)
```
```
saveRDS(teams, "C:/Users/Konrad/Desktop/ETM 527/RDS Data/Bike Teams/teams.rds")
```
## Ramya: Cleaning Bike Events
```
BikeEvents<-read.csv("C:\\Users\\Konrad\\Desktop\\ETM 527\\Events\\2013-2017 Bike Events.csv")
```
```
str(BikeEvents)
```
```
levels(BikeEvents$City)
```
```
#Cleaning cities
BikeEvents$City[BikeEvents$City == 'Athens, AL ']<- "Athens"
BikeEvents$City[BikeEvents$City == 'Cherry Hill, NJ with alternate start locations']<-"Cherry Hill"
#as.factor(BikeEvents$City[BikeEvents$City == 'Ridgeland, MS']<- "Ridgeland")
BikeEvents$City[BikeEvents$City == 'St. Augustine ']<-"St. Augustine"
BikeEvents$City[BikeEvents$City == 'Sioux Falls area']<-"Sioux Falls"
BikeEvents$City[BikeEvents$City == 'Schodack ']<-"Schodack"
BikeEvents$City[BikeEvents$City == 'San Antonio ']<-"San Antonio"
BikeEvents$City[BikeEvents$City == 'New York City']<-"New York"
BikeEvents$City[BikeEvents$City == 'New Albany ']<-"New Albany"
BikeEvents$City[BikeEvents$City == 'Keuka Park ']<-"Keuka Park"
BikeEvents$City[BikeEvents$City == 'Hollidaysburg, PA to State College']<-"Hollidaysburg"
BikeEvents$City[BikeEvents$City == 'Duluth to White Bear Lake']<-"Duluth"
BikeEvents$City[BikeEvents$City == 'Duluth to the Twin Cities']<-"Duluth"
BikeEvents$City[BikeEvents$City == 'Fargo area']<-"Fargo"
BikeEvents$City[BikeEvents$City == 'Dallas ']<-"Dallas"
BikeEvents$City[BikeEvents$City == 'DeKalb']<-"Dekalb"
BikeEvents$City[BikeEvents$City == 'DeKalb ']<-"Dekalb"
```
```
summary(BikeEvents$Event.Date)
```
```
#Date Conversion in R
datecols_bikeevents<-which(names(BikeEvents)%in%c("Event.Created.Date", "Event.Date"))
for (i in datecols_bikeevents) {
BikeEvents[,i] = as.Date(BikeEvents[,i], '%m/%d/%y')
}
```
## Saving the work
```
saveRDS(BikeEvents, "C:/Users/Konrad/Desktop/ETM 527/RDS Data/Bike Events/Bike Events.rds")
```
## Jordan: Cleaning Bike Participants
### Import data
```
participants <- read_csv("C:\\Users\\Konrad\\Desktop\\ETM 527\\Participants\\2013-2017 Bike MS Participants.csv")
```
### Basic data exploration
```
class(participants)
dim(participants)
glimpse(participants)
head(participants, n = 5)
tail(participants, n = 5)
summary(participants)
```
Able to see that some fields need to be converted from integers to characters. Also, easily able to identify which fields will need to be rid of NA values.
```
# need to change fields from int to char
participants$`Team ID` <- as.character(participants$`Team ID`)
class(participants$`Team ID`)
participants$`Contact ID` <- as.character(participants$`Contact ID`)
participants$`Address - Participant ZIP/Postal Code` <- as.character(participants$`Address - Participant ZIP/Postal Code`)
participants$`Event ID` <- as.character(participants$`Event ID`)
summary(participants)
#change event date from character to date-time
participants$`Event Date` <- mdy_hm(participants$`Event Date`)
class(participants$`Event Date`)
head(participants$`Event Date`, n = 5)
```
```
# acknowledge NA's
map(participants, ~sum(is.na(.)))
```
## Writing out Jordan's work
```
saveRDS(participants, "C:/Users/Konrad/Desktop/ETM 527/RDS Data/Participants/Participants.rds")
```
```
library(readr)
library(dplyr)
library(ggplot2)
donations2013 <- readRDS("C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2013.rds")
donations2014 <- readRDS("C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2014.rds")
donations2015 <- readRDS("C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2015.rds")
donations2016 <- readRDS("C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2016.rds")
donations2017 <- readRDS("C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2017.rds")
participants <- readRDS("C:/Users/Konrad/Desktop/ETM 527/RDS Data/Participants/Participants.rds")
str(participants)
```
# Creating Data Aggregations
## From donations data
```
gen_2013 <- summary(donations2013$`Donor Gender`)
gen_2014 <- summary(donations2014$`Donor Gender`)
gen_2015 <- summary(donations2015$`Donor Gender`)
gen_2016 <- summary(donations2016$`Donor Gender`)
gen_2017 <- summary(donations2017$`Donor Gender`)
gender_year <- tibble(c(2013, 2014, 2015, 2016, 2017),
c(gen_2013[1], gen_2014[1], gen_2015[1], gen_2016[1], gen_2017[1]),
c(gen_2013[2], gen_2014[2], gen_2015[2], gen_2016[2], gen_2017[2]),
c(gen_2013[3], gen_2014[3], gen_2015[3], gen_2016[3], gen_2017[3]))
colnames(gender_year) <- c("Year", "Female", "Male", "NA")
year_num <- tibble(2013:2017, c(nrow(donations2013), nrow(donations2014), nrow(donations2015), nrow(donations2016), nrow(donations2017)))
colnames(year_num) <- c("Year", "Number of Participants")
tot_gifts2013 <- sum(donations2013$`Gift Amount($)`, na.rm = TRUE)
tot_gifts2014 <- sum(donations2014$`Gift Amount($)`, na.rm = TRUE)
tot_gifts2015 <- sum(donations2015$`Gift Amount($)`, na.rm = TRUE)
tot_gifts2016 <- sum(donations2016$`Gift Amount($)`, na.rm = TRUE)
tot_gifts2017 <- sum(donations2017$`Gift Amount($)`, na.rm = TRUE)
med_gifts2013 <- median(donations2013$`Gift Amount($)`, na.rm = TRUE)
med_gifts2014 <- median(donations2014$`Gift Amount($)`, na.rm = TRUE)
med_gifts2015 <- median(donations2015$`Gift Amount($)`, na.rm = TRUE)
med_gifts2016 <- median(donations2016$`Gift Amount($)`, na.rm = TRUE)
med_gifts2017 <- median(donations2017$`Gift Amount($)`, na.rm = TRUE)
tot_donations <- tibble(2013:2017,
c(tot_gifts2013, tot_gifts2014, tot_gifts2015, tot_gifts2016, tot_gifts2017),
c(med_gifts2013, med_gifts2014, med_gifts2015, med_gifts2016, med_gifts2017))
colnames(tot_donations) <- c("Year", "Total Donations", "Median")
add_tot_gifts2013 <- sum(donations2013$`Additional Gift Amount($)`, na.rm = TRUE)
add_tot_gifts2014 <- sum(donations2014$`Additional Gift Amount($)`, na.rm = TRUE)
add_tot_gifts2015 <- sum(donations2015$`Additional Gift Amount($)`, na.rm = TRUE)
add_tot_gifts2016 <- sum(donations2016$`Additional Gift Amount($)`, na.rm = TRUE)
add_tot_gifts2017 <- sum(donations2017$`Additional Gift Amount($)`, na.rm = TRUE)
add_med_gifts2013 <- median(donations2013$`Additional Gift Amount($)`, na.rm = TRUE)
add_med_gifts2014 <- median(donations2014$`Additional Gift Amount($)`, na.rm = TRUE)
add_med_gifts2015 <- median(donations2015$`Additional Gift Amount($)`, na.rm = TRUE)
add_med_gifts2016 <- median(donations2016$`Additional Gift Amount($)`, na.rm = TRUE)
add_med_gifts2017 <- median(donations2017$`Additional Gift Amount($)`, na.rm = TRUE)
add_donations <- tibble(2013:2017,
c(add_tot_gifts2013, add_tot_gifts2014, add_tot_gifts2015, add_tot_gifts2016, add_tot_gifts2017),
c(add_med_gifts2013, add_med_gifts2014, add_med_gifts2015, add_med_gifts2016, add_med_gifts2017))
colnames(add_donations) <- c("Year", "Additional Donations", "Median")
```
## Plotting donations data
```
ggplot(data = subset(donations2013, !is.na(`Donor Gender`))) +
geom_bar(aes(`Donor Gender`, fill = `Donor Gender`), na.rm = TRUE) +
xlab("Donor Gender") +
ylab("Count") +
ggtitle("2013 NMS Participants by gender")
ggplot(data = subset(donations2013, !is.na(`Gift Payment Method`))) +
geom_bar(aes(`Gift Payment Method`, fill = `Donor Gender`)) +
xlab("Gift Payment Method") +
ylab("Dollars") +
ggtitle("2013 NMS Gift Payment Method Types by Gender")
library(scales)
ggplot(tot_donations, aes(Year, `Total Donations`/1000000)) +
geom_bar(stat = "identity") +
scale_y_continuous(labels=dollar_format(prefix="$")) +
ylab("Gift Amount ($M)") +
ggtitle("Gift amounts have fallen every year since 2014") +
theme(plot.title = element_text(hjust = 0.5)) +
ggsave("C:\\Users\\Konrad\\Desktop\\ETM 527\\Gift amounts have fallen.png")
ggplot(add_donations, aes(Year, `Additional Donations`/1000000)) +
geom_bar(stat = "identity") +
scale_y_continuous(labels=dollar_format(prefix="$")) +
ylab("Additional gift Amount ($M)") +
ggtitle("Additional gift amounts were increasing but fell in 2017") +
theme(plot.title = element_text(hjust = 0.5)) +
ggsave("C:\\Users\\Konrad\\Desktop\\ETM 527\\Additional gift amounts were increasing.png")
colnames(add_donations)
ggplot() +
geom_bar(data = tot_donations, aes(Year, `Total Donations` / 1000000), stat = "identity") +
geom_bar(data = add_donations, aes(Year, `Additional Donations` /1000000), stat = "identity", fill = "red", alpha = 0.6)
participants <- tibble(2013:2017, c(nrow(donations2013), nrow(donations2014), nrow(donations2015), nrow(donations2016), nrow(donations2017)))
colnames(participants) <- c("Year", "Participants")
ggplot(participants) +
geom_bar(aes(x = Year, y = Participants), stat = "identity") +
ylim(0, 1000000) +
scale_y_continuous(labels= comma) +
ggtitle("Number of participants has fallen every year since 2013") +
theme(plot.title = element_text(hjust = 0.5)) +
ggsave("C:\\Users\\Konrad\\Desktop\\ETM 527\\Number of participants has fallen.png")
don_per_part = tibble(2013:2017, tot_donations$`Total Donations` / participants$Participants)
colnames(don_per_part) <- c("Year", "Average Donation")
ggplot(don_per_part, aes(Year, `Average Donation`)) +
geom_bar(stat = "identity") +
ylab("Averate Gift Amount") +
ggtitle("Average gift per participant has increased every year since 2013") +
theme(plot.title = element_text(hjust = 0.5)) +
ggsave("C:\\Users\\Konrad\\Desktop\\ETM 527\\Average gift per participant has increased.png")
```
## Ramya's Bike Events Data Aggregation
```
#Average amount by total participants in all the states
Amount_by_States<-aggregate(BikeEvents$Total.From.Participant..., by=list(State = BikeEvents$State), FUN = mean)
head(Amount_by_States)
```
```
#Amount raised by participants from 2013-2017
Amount_by_year <-aggregate(BikeEvents$Total.From.Participant..., by = list(year = BikeEvents$Fiscal.Year), sum)
Amount_by_year
```
```
#selft donars over
Donors_by_year <-aggregate(BikeEvents$Total.From.Participant..., by = list(year = BikeEvents$Fiscal.Year), sum)
Donors_by_year
```
```
#Active registrations overs the years
Active_registrations <-aggregate(BikeEvents$Active.Registrations, by = list(year = BikeEvents$Fiscal.Year), sum)
Active_registrations
```
## Ramya's Plots
```
library('ggplot2')
Amounts_ggplot<-ggplot(Amount_by_States, aes(x=Amount_by_States$State,y=Amount_by_States$x))+
theme(axis.text.x = element_text(face = "bold", size = 8, angle = 90))+
geom_bar(stat = 'identity', fill = "blue")+
ggtitle("Average amount raised by Bike Events participants across all the states") +
labs(x ='States', y = 'Average amount')
```
```
Amounts_ggplot
ggsave(filename="D:/527- Data Mining/TUN Data Challenge/Data Visualizations/Amounts_ggplot.pdf", plot=Amounts_ggplot)
```
```
ggplot(Amount_by_year, aes(x = Amount_by_year$year, y = Amount_by_year$x)) +
geom_bar(stat = 'identity', fill = "blue")+ggtitle("Participants amount to Bike events slightly decreasing since 2014") +
labs(x ='Year', y = 'Total amount')
```
```
#Donars by year
ggplot(Donors_by_year, aes(x = Donors_by_year$year, y = Donors_by_year$x))+
geom_bar(stat = 'identity', fill = "blue")+ggtitle("Self donors gradually decreasing since 2014 ") +
labs(x ='Year', y = 'Total amount')
```
```
#Donars by year
ggplot(Active_registrations, aes(x = Active_registrations$year, y = Active_registrations$x)) +
geom_bar(stat = 'identity', fill = "blue")+ggtitle("Active registrations have fallen since 2013 ") +
labs(x ='Year', y = 'No of registrations')
```
## Participants EDA
```
# some basic data viz
barplot(table(participants$`Participant Occupation`), ylab = "Count", main = "Occupations of our Participants",
ylim = c(0,2500), las = 2, col=rgb(0.2,0.4,0.6,0.6))
occ.count <- table(participants$`Participant Occupation`)
occ.count
occupation.viz <- ggplot(data = participants, aes(x = participants$`Participant Occupation`)) +
geom_bar(aes(y = (..count..), fill = `Participant Gender`)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
occupation.viz + labs(x = "Occupations") +
labs(title = "Frequency of Participant Occupation") +
labs(y = "Count")
```
```
library(readr)
library(dplyr)
library(ggplot2)
library(scales)
donations2013 <- readRDS("C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2013.rds")
donations2014 <- readRDS("C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2014.rds")
donations2015 <- readRDS("C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2015.rds")
donations2016 <- readRDS("C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2016.rds")
donations2017 <- readRDS("C:/Users/Konrad/Desktop/ETM 527/RDS Data/Donations/Donations2017.rds")
participants <- readRDS("C:/Users/Konrad/Desktop/ETM 527/RDS Data/Participants/Participants.rds")
#summary(donations2017$`Donor Gender`)
#levels(donations2017$`Donor Gender`) <- list(Male = "Male", Female = "Female", None = "")
```
# Creating Data Aggregations
## From donations data
```
# gen_2013 <- summary(donations2013$`Donor Gender`)
# gen_2014 <- summary(donations2014$`Donor Gender`)
# gen_2015 <- summary(donations2015$`Donor Gender`)
# gen_2016 <- summary(donations2016$`Donor Gender`)
# gen_2017 <- summary(donations2017$`Donor Gender`)
#
#
# gender_year <- tibble(c(2013, 2014, 2015, 2016, 2017),
# c(gen_2013[1], gen_2014[1], gen_2015[1], gen_2016[1], gen_2017[1]),
# c(gen_2013[2], gen_2014[2], gen_2015[2], gen_2016[2], gen_2017[2]),
# c(gen_2013[3], gen_2014[3], gen_2015[3], gen_2016[3], gen_2017[3]),
# c(gen_2013[4], gen_2014[4], gen_2015[4], gen_2016[4], gen_2017[4]))
#
# colnames(gender_year) <- c("Year", "Female", "Male", "None", "NA")
#
#
# year_num <- tibble(2013:2017, c(nrow(donations2013), nrow(donations2014), nrow(donations2015), nrow(donations2016), nrow(donations2017)))
# colnames(year_num) <- c("Year", "Number of Participants")
#
#
# tot_gifts2013 <- sum(donations2013$`Gift Amount($)`, na.rm = TRUE)
# tot_gifts2014 <- sum(donations2014$`Gift Amount($)`, na.rm = TRUE)
# tot_gifts2015 <- sum(donations2015$`Gift Amount($)`, na.rm = TRUE)
# tot_gifts2016 <- sum(donations2016$`Gift Amount($)`, na.rm = TRUE)
# tot_gifts2017 <- sum(donations2017$`Gift Amount($)`, na.rm = TRUE)
#
# med_gifts2013 <- median(donations2013$`Gift Amount($)`, na.rm = TRUE)
# med_gifts2014 <- median(donations2014$`Gift Amount($)`, na.rm = TRUE)
# med_gifts2015 <- median(donations2015$`Gift Amount($)`, na.rm = TRUE)
# med_gifts2016 <- median(donations2016$`Gift Amount($)`, na.rm = TRUE)
# med_gifts2017 <- median(donations2017$`Gift Amount($)`, na.rm = TRUE)
#
# mean_gifts2013 <- mean(donations2013$`Gift Amount($)`, na.rm = TRUE)
# mean_gifts2014 <- mean(donations2014$`Gift Amount($)`, na.rm = TRUE)
# mean_gifts2015 <- mean(donations2015$`Gift Amount($)`, na.rm = TRUE)
# mean_gifts2016 <- mean(donations2016$`Gift Amount($)`, na.rm = TRUE)
# mean_gifts2017 <- mean(donations2017$`Gift Amount($)`, na.rm = TRUE)
#
#
# tot_donations <- tibble(2013:2017,
# c(tot_gifts2013, tot_gifts2014, tot_gifts2015, tot_gifts2016, tot_gifts2017),
# c(med_gifts2013, med_gifts2014, med_gifts2015, med_gifts2016, med_gifts2017),
# c(mean_gifts2013, mean_gifts2014, mean_gifts2015, mean_gifts2016, mean_gifts2017))
#
# colnames(tot_donations) <- c("Year", "Total Donations", "Median", "Mean")
#
# add_tot_gifts2013 <- sum(donations2013$`Additional Gift Amount($)`, na.rm = TRUE)
# add_tot_gifts2014 <- sum(donations2014$`Additional Gift Amount($)`, na.rm = TRUE)
# add_tot_gifts2015 <- sum(donations2015$`Additional Gift Amount($)`, na.rm = TRUE)
# add_tot_gifts2016 <- sum(donations2016$`Additional Gift Amount($)`, na.rm = TRUE)
# add_tot_gifts2017 <- sum(donations2017$`Additional Gift Amount($)`, na.rm = TRUE)
#
# add_med_gifts2013 <- median(donations2013$`Additional Gift Amount($)`, na.rm = TRUE)
# add_med_gifts2014 <- median(donations2014$`Additional Gift Amount($)`, na.rm = TRUE)
# add_med_gifts2015 <- median(donations2015$`Additional Gift Amount($)`, na.rm = TRUE)
# add_med_gifts2016 <- median(donations2016$`Additional Gift Amount($)`, na.rm = TRUE)
# add_med_gifts2017 <- median(donations2017$`Additional Gift Amount($)`, na.rm = TRUE)
#
# add_mean_gifts2013 <- mean(donations2013$`Additional Gift Amount($)`, na.rm = TRUE)
# add_mean_gifts2014 <- mean(donations2014$`Additional Gift Amount($)`, na.rm = TRUE)
# add_mean_gifts2015 <- mean(donations2015$`Additional Gift Amount($)`, na.rm = TRUE)
# add_mean_gifts2016 <- mean(donations2016$`Additional Gift Amount($)`, na.rm = TRUE)
# add_mean_gifts2017 <- mean(donations2017$`Additional Gift Amount($)`, na.rm = TRUE)
#
#
# add_donations <- tibble(2013:2017,
# c(add_tot_gifts2013, add_tot_gifts2014, add_tot_gifts2015, add_tot_gifts2016, add_tot_gifts2017),
# c(add_med_gifts2013, add_med_gifts2014, add_med_gifts2015, add_med_gifts2016, add_med_gifts2017),
# c(add_mean_gifts2013, add_mean_gifts2014, add_mean_gifts2015, add_mean_gifts2016, add_mean_gifts2017))
#
# colnames(add_donations) <- c("Year", "Additional Donations", "Median", "Mean")
#
#
# write_rds(tot_donations, "C:/Users/Konrad/Desktop/ETM 527/Calculated/Gift Amounts.rds")
# write_rds(add_donations, "C:/Users/Konrad/Desktop/ETM 527/Calculated/Additional Amounts.rds")
# write_rds(gender_year, "C:/Users/Konrad/Desktop/ETM 527/Calculated/Yearly Gender Breakdown.rds")
#
# gen_tot_2013_gifts <- aggregate(donations2013$`Gift Amount($)`, by = list(donations2013$`Donor Gender`), FUN = sum)
# gen_med_2013_gifts <- aggregate(donations2013$`Gift Amount($)`, by = list(donations2013$`Donor Gender`), FUN = median)
# gen_mean_2013_gifts <- aggregate(donations2013$`Gift Amount($)`, by = list(donations2013$`Donor Gender`), FUN = mean)
#
# gen_tot_2014_gifts <- aggregate(donations2014$`Gift Amount($)`, by = list(donations2014$`Donor Gender`), FUN = sum)
# gen_med_2014_gifts <- aggregate(donations2014$`Gift Amount($)`, by = list(donations2014$`Donor Gender`), FUN = median)
# gen_mean_2014_gifts <- aggregate(donations2014$`Gift Amount($)`, by = list(donations2014$`Donor Gender`), FUN = mean)
#
# gen_tot_2015_gifts <- aggregate(donations2015$`Gift Amount($)`, by = list(donations2015$`Donor Gender`), FUN = sum)
# gen_med_2015_gifts <- aggregate(donations2015$`Gift Amount($)`, by = list(donations2015$`Donor Gender`), FUN = median)
# gen_mean_2015_gifts <- aggregate(donations2015$`Gift Amount($)`, by = list(donations2015$`Donor Gender`), FUN = mean)
#
# gen_tot_2016_gifts <- aggregate(donations2016$`Gift Amount($)`, by = list(donations2016$`Donor Gender`), FUN = sum)
# gen_med_2016_gifts <- aggregate(donations2016$`Gift Amount($)`, by = list(donations2016$`Donor Gender`), FUN = median)
# gen_mean_2016_gifts <- aggregate(donations2016$`Gift Amount($)`, by = list(donations2016$`Donor Gender`), FUN = mean)
#
# gen_tot_2017_gifts <- aggregate(donations2017$`Gift Amount($)`, by = list(donations2017$`Donor Gender`), FUN = sum)
# gen_med_2017_gifts <- aggregate(donations2017$`Gift Amount($)`, by = list(donations2017$`Donor Gender`), FUN = median)
# gen_mean_2017_gifts <- aggregate(donations2017$`Gift Amount($)`, by = list(donations2017$`Donor Gender`), FUN = mean)
#
# gender_year_totals <- rbind(gen_tot_2013_gifts, gen_tot_2014_gifts, gen_tot_2015_gifts, gen_tot_2016_gifts, gen_tot_2017_gifts)
#
# gender_year_med <- rbind(gen_med_2013_gifts, gen_med_2014_gifts, gen_med_2015_gifts, gen_med_2016_gifts, gen_med_2017_gifts)
#
# gender_year_mean <- rbind(gen_mean_2013_gifts, gen_mean_2014_gifts, gen_mean_2015_gifts, gen_mean_2016_gifts, gen_mean_2017_gifts)
#
# gender_totals <- tibble(rep(2013:2017, each = 3),
# rep(c("Female", "Male", "None"), times = 5),
# gender_year_totals$x,
# gender_year_med$x,
# gender_year_mean$x)
#
# colnames(gender_totals) <- c("Year", "Gender", "Total Gifts", "Median", "Mean")
#
# write_rds(gender_totals, "C:/Users/Konrad/Desktop/ETM 527/Calculated/Gender Year Totals Breakdown.rds")
```
```
tot_donations <- read_rds("C:/Users/Konrad/Desktop/ETM 527/Calculated/Gift Amounts.rds")
add_donations <- read_rds("C:/Users/Konrad/Desktop/ETM 527/Calculated/Additional Amounts.rds")
gender_year <- read_rds("C:/Users/Konrad/Desktop/ETM 527/Calculated/Yearly Gender Breakdown.rds")
gender_totals <- read_rds("C:/Users/Konrad/Desktop/ETM 527/Calculated/Gender Year Totals Breakdown.rds")
```
##### Total Donations
```
ggplot(tot_donations, aes(Year, `Total Donations`)) +
geom_bar(stat = "identity") +
scale_y_continuous(labels = comma) +
ylab("Participants") +
ggtitle("The number of participants has fallen every year since 2014") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size = 18, face = "bold")) #+
#ggsave("C:\\Users\\Konrad\\Desktop\\ETM 527\\Participant numbers fallen.png")
ggplot(participants, aes(`Fiscal Year`)) +
geom_bar() +
scale_y_continuous(labels = comma) +
ylab("Participants") +
xlab("Fiscal Year") +
ggtitle("Number of participants has decreased every year") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size =18, face = "bold")) #+
#ggsave("C:\\Users\\Konrad\\Desktop\\ETM 527\\Number of participants decreased every year.png", width = 10)
```
## Sum of Donations
```
ggplot(tot_donations, aes(Year, `Total Donations`/1000000)) +
geom_bar(stat = "identity") +
scale_y_continuous(labels=dollar_format(prefix="$")) +
ylab("Gift Amount ($M)") +
ggtitle("Total gift amounts have fallen every year since 2014") +
theme(plot.title = element_text(hjust = 0.5)) #+
#ggsave("C:\\Users\\Konrad\\Desktop\\ETM 527\\Gift amounts have fallen.png")
```
### Mean Donations
```
ggplot(tot_donations, aes(Year, Mean)) +
geom_bar(stat = "identity") +
scale_y_continuous(labels=dollar_format(prefix="$")) +
ylab("Gift Amount") +
ggtitle("Mean gift amount continues to rise") +
theme(plot.title = element_text(hjust = 0.5)) #+
#ggsave("C:\\Users\\Konrad\\Desktop\\ETM 527\\Gift amounts have fallen.png")
```
### Median Donations
```
ggplot(tot_donations, aes(Year, Median)) +
geom_bar(stat = "identity") +
scale_y_continuous(labels=dollar_format(prefix="$")) +
ylab("Gift Amount") +
ggtitle("Median gift amount remains unchanged") +
theme(plot.title = element_text(hjust = 0.5)) #+
#ggsave("C:\\Users\\Konrad\\Desktop\\ETM 527\\Gift amounts have fallen.png")
```
##### Gender Totals
```
ggplot(gender_totals[order(gender_totals$Gender),], aes(Year, `Total Gifts`/1000000, fill = Gender)) +
geom_bar(stat = "identity") +
scale_y_continuous(labels=dollar_format(prefix="$", suffix = "M")) +
scale_fill_manual(values=c("#A92A2A", "#205A8F", "#B4BFC3")) +
ylab("Gift Amount (M)") +
ggtitle("Total gift amount has decreased") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size =18, face = "bold")) #+
#ggsave("C:\\Users\\Konrad\\Desktop\\ETM 527\\Total gift amounts by gender.png", width = 12)
ggplot(gender_totals, aes(Year, Median, fill = Gender)) +
geom_bar(stat = "identity", position = "dodge") +
scale_y_continuous(labels=dollar_format(prefix="$")) +
scale_fill_manual(values=c("#A92A2A", "#205A8F", "#B4BFC3")) +
ylab("Gift Amount ($)") +
ggtitle("Median gift amount remains unchanged") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size =18, face = "bold")) #+
#ggsave("C:\\Users\\Konrad\\Desktop\\ETM 527\\Median gift amounts by gender.png", width = 12)
ggplot(gender_totals, aes(Year, Mean, fill = Gender)) +
geom_bar(stat = "identity", position = "dodge") +
scale_y_continuous(labels=dollar_format(prefix="$")) +
scale_fill_manual(values = c("#A92A2A", "#205A8F", "#B4BFC3")) +
ylab("Gift Amount ($)") +
ggtitle("Mean gift amount has fluctuated") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size =18, face = "bold")) #+
#ggsave("C:\\Users\\Konrad\\Desktop\\ETM 527\\Mean gift amounts by gender.png", width = 12)
```
```
library(dplyr)
library(magrittr)
library(tidyr)
library(knitr)
library(tibble)
library(readxl)
library(graphics)
library(ggplot2)
library(scales)
library(RColorBrewer)
```
```
nt <-
read_excel("C:/Users/eva/Documents/ETM 527/Data Challenge 2018/National Teams/2013-2017 National Team Activity.xlsx", skip = 1, na = c("NA", " ", "-999"),
col_types = c("text", "text", "text",
"text", "text", "text", "date", "text",
"numeric", "numeric", "numeric",
"numeric", "numeric", "text", "text",
"text", "text", "text", "text", "text",
"text", "numeric", "text", "text",
"text", "text", "text"))
dim(nt)
```
```
#Removing the "Corporate Name" field as it was blank for all recornt
nt$'Corporate Name' <- NULL
str(nt)
```
```
nt$`Event Chapter` <- as.factor(nt$`Event Chapter`)
nt$`Event Category` <- as.factor(nt$`Event Category`)
nt$`Event Type` <- as.factor(nt$`Event Type`)
nt$`Local Team Name` <- as.factor(nt$`Local Team Name`)
nt$`Event Name` <- as.factor(nt$`Event Name`)
nt$Location <- as.factor(nt$Location)
cols_to_factor <- 12:ncol(nt)
nt[, cols_to_factor] <- lapply(nt[, cols_to_factor], as.factor)
```
```
# Fill down team names
for (i in 2:nrow(nt))
{
if (is.na(nt$`National Team Name`[i]) & !is.na(nt$`National Team Name`[i-1]))
{
nt$`National Team Name`[i] <- nt$`National Team Name`[i-1]
}
}
View(nt)
```
```
# add NA in the Team Name where applicable
nt$`National Team Name` <- as.factor(nt$`National Team Name`)
summary(nt$`National Team Name`)
nt$`National Team Name`[nt$`National Team Name` == "N/A"] <- NA #selects all matching rows and changes to NA
summary(nt$`National Team Name`)
```
```
Not.National.Team = subset(nt, is.na(nt$`National Team Name`), select =`National Team Name`:`Primary Connection To MS`)
Not.National.Team = Not.National.Team[,c(5,6,2,3,4,7:26)]
nt <- nt[which(!is.na(nt$`National Team Name`)),]
nt <- nt[which(!is.na(nt$`Local Team Name`)),]
```
```
NameLocation <- nt$Location
summary(NameLocation)
BlankLocation <- nt[which(is.na(nt$Location)), ]
View(BlankLocation)
```
```
```
```
View(nt)
sink("C:/Users/eva/Documents/ETM 527/Data Challenge 2018/National Teams/National Teams Basic Exploration.txt")
class(nt)
dim(nt)
glimpse(nt)
head(nt)
tail(nt)
summary(nt)
str(nt)
sink()
```
```