-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ascent_tab.R
170 lines (147 loc) · 6.9 KB
/
Ascent_tab.R
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
library(tidyverse)
library(scales)
library(timetk)
source("source_data.R")
# Calculations for the Ascent tab
### Create the base data set
Ascent_base <- Weekly_data |>
select(Date,
Total_Ascent,
Ascent,
AscentOnline) |>
rename(Total = Total_Ascent,
In_person = Ascent,
Online = AscentOnline)
# Ascent Main Table
Ascent_Last_week <- tail(Ascent_base, 1) |>
select(Total,
In_person,
Online)
# Weekly mean month-to-date for current month
Ascent_Month_To_Date <- Ascent_base |>
select(Date,
Total,
In_person,
Online) |>
group_by(floor_date(Date, "month")) |>
summarize(In_person = round(mean(In_person, na.rm = TRUE), 2),
Online = round(mean(Online, na.rm = TRUE), 2),
Total = round(mean(Total, na.rm = TRUE), 2)) |>
filter(row_number() == n())
# Calculate the weekly average for the same month the previous year
Ascent_Year_over_year <- Ascent_base |>
select(Date,
In_person,
Online,
Total) |>
group_by(floor_date(Date, "month")) |>
summarize(In_person = round(mean(In_person, na.rm = TRUE), 2),
Online = round(mean(Online, na.rm = TRUE), 2),
Total = round(mean(Total, na.rm = TRUE), 2)) |>
slice_tail(n = 13) |>
slice_head(n = 1)
# Absolute year-over-year change and percent change
Ascent_YoY_Month <- data.frame(Total = round(Ascent_Month_To_Date$Total - Ascent_Year_over_year$Total, 1),
In_person = round(Ascent_Month_To_Date$In_person - Ascent_Year_over_year$In_person, 1),
Online = round(Ascent_Month_To_Date$Online - Ascent_Year_over_year$Online, 1))
Ascent_YoY_Month <- Ascent_YoY_Month |>
mutate(Total_percent = Ascent_YoY_Month$Total / Ascent_Year_over_year$Total,
In_person_percent = Ascent_YoY_Month$In_person / Ascent_Year_over_year$In_person,
Online_percent = Ascent_YoY_Month$Online / Ascent_Year_over_year$Online)
# Year-to-date averages for current and past year
Ascent_By_week <- Weekly_data |>
mutate(Year = year(Date)) |>
mutate(Month = month(Date, label = TRUE))
Ascent_End_week <- tail(Ascent_By_week$Week, 1)
Ascent_Current_year <- tail(Ascent_By_week$Year, 1)
Ascent_Previous_year <- Ascent_Current_year - 1
Ascent_Current_month <- tail(Ascent_By_week$Month, 1)
Ascent_Attendance_past_year <- subset(Ascent_By_week, Year == Previous_year & Week <= End_week)
Ascent_Attendance_current_year <- subset(Ascent_By_week, Year == Current_year)
Ascent_Weekly_ave_past_year <- Ascent_Attendance_past_year |>
group_by(Year) |>
summarise(Total = round(mean(Total_Ascent), 1),
In_person = round(mean(Ascent), 1),
Online = round(mean(AscentOnline), 1))
Ascent_Weekly_ave_current_year <- Attendance_current_year |>
group_by(Year) |>
summarise(Total = round(mean(Total_Ascent), 2),
In_person = round(mean(Ascent), 2),
Online = round(mean(AscentOnline), 2))
# Ascent summary table
Ascent_main_table <- matrix(c(
Ascent_Last_week$Total,
Ascent_Last_week$In_person,
Ascent_Last_week$Online,
Ascent_Month_To_Date$Total,
Ascent_Month_To_Date$In_person,
Ascent_Month_To_Date$Online,
Ascent_Year_over_year$Total,
Ascent_Year_over_year$In_person,
Ascent_Year_over_year$Online,
Ascent_YoY_Month$Total,
Ascent_YoY_Month$In_person,
Ascent_YoY_Month$Online,
label_percent(accuracy = 0.1)(Ascent_YoY_Month$Total_percent),
label_percent(accuracy = 0.1)(Ascent_YoY_Month$In_person_percent),
label_percent(accuracy = 0.1)(Ascent_YoY_Month$Online_percent)),
ncol = 5,
byrow = FALSE
)
colnames(Ascent_main_table) <- c("Last week",
paste(Ascent_Current_month, Ascent_Current_year, sep = " "),
paste(Ascent_Current_month, Ascent_Previous_year, sep = " "),
"Change",
"Percent change")
rownames(Ascent_main_table) <- c("Combined",
"In person",
"Online")
# Year-to-date averages, absolute change, and percent change table
Ascent_ytd_change <- data.frame(Total = Ascent_Weekly_ave_current_year$Total - Ascent_Weekly_ave_past_year$Total,
In_person = Ascent_Weekly_ave_current_year$In_person - Ascent_Weekly_ave_past_year$In_person,
Online = Ascent_Weekly_ave_current_year$Online - Ascent_Weekly_ave_past_year$Online)
Ascent_ytd_change <- Ascent_ytd_change |>
mutate(Total_percent = Ascent_ytd_change$Total / Ascent_Weekly_ave_past_year$Total,
In_person_percent = Ascent_ytd_change$In_person / Ascent_Weekly_ave_past_year$In_person,
Online_percent = Ascent_ytd_change$Online / Ascent_Weekly_ave_past_year$Online)
Ascent_ytd_table <- matrix(c(Ascent_Weekly_ave_current_year$Total,
Ascent_Weekly_ave_current_year$In_person,
Ascent_Weekly_ave_current_year$Online,
Ascent_Weekly_ave_past_year$Total,
Ascent_Weekly_ave_past_year$In_person,
Ascent_Weekly_ave_past_year$Online,
round(Ascent_ytd_change$Total, 1),
round(Ascent_ytd_change$In_person, 1),
round(Ascent_ytd_change$Online, 1),
label_percent(accuracy = 0.1)(Ascent_ytd_change$Total_percent),
label_percent(accuracy = 0.1)(Ascent_ytd_change$In_person_percent),
label_percent(accuracy = 0.1)(Ascent_ytd_change$Online_percent)),
ncol = 4,
byrow = FALSE
)
colnames(Ascent_ytd_table) <- c(paste(Current_year),
paste(Previous_year),
"Change",
"Percent change")
rownames(Ascent_ytd_table) <- c("Combined",
"In person",
"Online")
# Data sets for reactive selection
## Ascent In person
Ascent_in_person <- Ascent_base |>
select(Date,
In_person) |>
group_by(Date = floor_date(Date, "month")) |>
summarize(attendance = round(mean(In_person, na.rm = TRUE), 2))
## Ascent online
Ascent_online <- Ascent_base |>
select(Date,
Online) |>
group_by(Date = floor_date(Date, "month")) |>
summarise(attendance = round(mean(Online, na.rm = TRUE), 2))
## Ascent combined
Ascent_total <- Ascent_base |>
select(Date,
Total) |>
group_by(Date = floor_date(Date, "month")) |>
summarise(attendance = round(mean(Total, na.rm = TRUE), 2))