-
Notifications
You must be signed in to change notification settings - Fork 0
/
collating_budget_heads.R
143 lines (119 loc) · 4.83 KB
/
collating_budget_heads.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
source("scripts/libraries.R")
source("scripts/functions.R")
# Which heads to include --------------------------------------------------
head_summary <-
readr::read_csv(
"datasets/state-budgets/assam/comparing_head_codes_across_years_2022.csv",
col_types = cols()
)
cols_to_check <-
c("2018-19", "2019-20", "2020-21", "2021-22", "2022-23")
head_summary$include_in_analysis <- 1
for(i in 1:nrow(head_summary)){
col_status <- head_summary[i,cols_to_check] %>% t()
if(FALSE %in% col_status){
head_summary$include_in_analysis[i] <- 0
}
}
all_levels <- head_summary$level %>% unique()
# Only include levels 1 to 6
levels_to_include <- paste0("level_",2:6,"_code")
head_summary_sub <-
head_summary[head_summary$level %in% levels_to_include, ]
# Only include where include_in_analysis is 1
head_summary_sub_sub <-
head_summary_sub[head_summary_sub$include_in_analysis == 1, ]
# Creating grant wise time series -----------------------------------------------
all_heads <-
readr::read_csv("datasets/state-budgets/assam/all_heads_list_2022.csv",
col_types = cols(.default = "c"))
all_categories <- unique(head_summary_sub_sub$category)
all_levels <- unique(head_summary_sub_sub$level)
budget_timeseries <- c()
for(i in 1:length(all_categories)){
cat_i <- all_categories[[i]]
for(j in 1:length(all_levels)){
level_j <- all_levels[[j]]
level_codes <-
head_summary_sub_sub$unique_level_heads[head_summary_sub_sub$category ==
cat_i &
head_summary_sub_sub$level == level_j] %>% unique()
for(k in 1:length(level_codes)){
level_codes_k <- level_codes[[k]]
print_string <- glue::glue("{cat_i} | {level_j} | {level_codes_k}")
print(print_string)
budget_data <- all_heads[all_heads[,level_j] == level_codes_k & all_heads$budget_for == cat_i,]
level_col <- stringr::str_replace(level_j,pattern = "_code",replacement = "")
budget_cols <- c("budget_for","year", level_col,level_j,"actuals","estimate_py","revised_py","estimate_cy")
budget_data_sub <- budget_data[,budget_cols]
names(budget_data_sub)[] <-
c(
"budget_for",
"year",
"head_title",
"level_code",
"actuals",
"estimate_py",
"revised_py",
"estimate_cy"
)
budget_data_sub$actuals <- as.numeric(budget_data_sub$actuals)
budget_data_sub$estimate_py <- as.numeric(budget_data_sub$estimate_py)
budget_data_sub$revised_py <- as.numeric(budget_data_sub$revised_py)
budget_data_sub$estimate_cy <- as.numeric(budget_data_sub$estimate_cy)
budget_data_agg <-
budget_data_sub %>%
group_by(budget_for, year, head_title, level_code) %>%
summarise(
total_actuals = sum(actuals)/100,
total_estimate_py = sum(estimate_py)/100,
total_revised_py = sum(revised_py)/100,
total_estimate_cy = sum(estimate_cy)/100
)
budget_data_agg$level_col <- level_col
budget_timeseries <-
dplyr::bind_rows(budget_timeseries, budget_data_agg)
# For converting the aggregate dataset from wide to long format
# budget_data_agg_long <-
# pivot_longer(
# data = budget_data_agg,
# names_to = "indicator",
# values_to = "value",
# cols = c(
# "total_actuals" ,
# "total_estimate_py",
# "total_revised_py" ,
# "total_estimate_cy"
# )
# )
}
}
}
# Removing rows where actual expenditure is NA -----------------------------------
# In the year 2018-19, the same budget head code is present for two different budget heads
# code is - 4059__1
# We're removing all rows that start with this budget head and where the actual expenditure is NA.
# This will remove 5 rows from the final dataset
heads_to_remove <-
budget_timeseries$head_title[is.na(budget_timeseries$total_actuals)]
budget_timeseries <-
budget_timeseries[!budget_timeseries$head_title %in% heads_to_remove, ]
readr::write_csv(budget_timeseries,"datasets/state-budgets/assam/budget_timeseries_2022.csv")
# Update code level summary -----------------------------------------------
code_level_summary <- readr::read_csv("datasets/state-budgets/assam/code_level_summary_2022.csv", col_types = cols())
# Only include till level 6
code_level_summary_l6 <- code_level_summary[code_level_summary$level %in% paste0("level_",2:6,"_code"),]
code_level_summary_l6$total_values <- NULL
code_level_summary_l6$common_values <- NULL
code_level_summary_l6_wide <-
pivot_wider(
data = code_level_summary_l6,
id_cols = c("category", "major_head"),
names_from = "level",
values_from = "percent_common"
)
readr::write_csv(
code_level_summary_l6_wide,
"datasets/state-budgets/assam/major_head_coverage_2022.csv"
)
knitr::kable(code_level_summary_l6_wide, format = "pipe") %>% clipr::write_clip()