-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfact.R
More file actions
216 lines (152 loc) · 8.89 KB
/
fact.R
File metadata and controls
216 lines (152 loc) · 8.89 KB
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
list.of.packages <- c("data.table","readr","dplyr","DescTools","stringr")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
lapply(list.of.packages, require, character.only=T)
wd='~/git/ddw-r-scripts'
setwd(wd)
#This file can only be processed after fact table has been processed successfully
source("baseYearConstants.R")
#source("connect.R")
#Load oecd_crs data set that will be manipulated
crs <- fread('~/ddw_update/mirrors/crs_mirror.csv',na.strings="")
#donor_map <- ddw('dimension.oecd_donor_to_di_id_map');
#recipient_to_di_id_map <- ddw('dimension.oecd_recipient_to_di_id_map');
#sector_3_code_map <- ddw('dimension.oecd_crs_sector_code_3_digit_to_itep_sector_web_id_map')
#channel_code_5_web_map <- ddw('dimension.oecd_crs_channel_code_5_digit_to_itep_channel_web_id_map')
donor_map <- read.csv('output/dimension.oecd_donor_to_di_id_map');
recipient_to_di_id_map <- read.csv('output/dimension.oecd_recipient_to_di_id_map');
sector_3_code_map <- read.csv('output/dimension.oecd_crs_sector_code_3_digit_to_itep_sector_web_id_map')
channel_code_5_web_map <- read.csv('output/dimension.oecd_crs_channel_code_5_digit_to_itep_channel_web_id_map')
#Get only oda from crs
#Filters that will be used to get only ODA
flow_code_v <- c(11,13,19)
category_v <- 10
oda_filter <- crs[flow_code %in% flow_code_v & category == category_v & !is.na(usd_disbursement) & usd_disbursement != 0]
oda_filter$oda_donor_bundle <- 'E'
# Get vectors to generate ODA bundle codes
# Bundle Code F
aid_type_v <- c('E01','E02','G01','H01','H02')
finance_type_v <- c(210)
sector_code_v <- c(600)
purpose_code_v <- c(93010,99820)
#oda_filter$oda_donor_bundle = NA
#oda_filter[,oda_donor_bundle := as.character(oda_donor_bundle)]
# Filter using aid_type,finance_type,sector_code and purpose-code
oda_filter$oda_donor_bundle[which(oda_filter$aid_type %in% aid_type_v | oda_filter$finance_type %in% finance_type_v |
oda_filter$sector_code %in% sector_code_v |
oda_filter$purpose_code %in% purpose_code_v)] <- 'F'
# Bundle Code A
'%!in%' <- function(x,y)!('%in%'(x,y))
ignore_bundle_code <- c('F')
flow_code_v <- c(13,19)
oda_filter$oda_donor_bundle[which(oda_filter$flow_code %in% flow_code_v
& oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'A'
# Bundle G
ignore_bundle_code <- append(ignore_bundle_code,'A')
aid_type_v <- 'D%'
ftc_v <- 1
oda_filter$oda_donor_bundle[which((oda_filter$aid_type %like% aid_type_v |
oda_filter$ftc == ftc_v) & oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'G'
# Bundle Code B
ignore_bundle_code <- append(ignore_bundle_code,'G')
aid_type_v <- c('A01','A02')
oda_filter$oda_donor_bundle[which(oda_filter$aid_type %in% aid_type_v &
oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'B'
# Bundle Code C
ignore_bundle_code <- append(ignore_bundle_code,'B')
purpose_code_v <- c(52010,53030,53040,72040)
oda_filter$oda_donor_bundle[which(oda_filter$purpose_code %in% purpose_code_v &
oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'C'
# Bundle Code B
ignore_bundle_code <- append(ignore_bundle_code,'C')
# Check for aid type and recipient code
aid_type_v <- c('B01','B03','B04')
oda_filter$oda_donor_bundle[which(oda_filter$aid_type %in% aid_type_v & oda_filter$recipient_code != 9998 &
oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'B'
# Bundle Code D
# aid type B01 or B03 or B04 and recipient code 998
aid_type_v <- c('B01','B03','B04')
oda_filter$oda_donor_bundle[which(oda_filter$recipient_code == 9998 &
oda_filter$aid_type %in% aid_type_v &
oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'D'
# Bundle Code B
ignore_bundle_code <- append(ignore_bundle_code,'D')
purpose_code_v <- 51010
pba_v <- 1
# also check for combination of aid_type and recipient code
# aid_type B01 or B03 or Bo4 and !recipient code 998
#channel channel code to character and check if
channel_code_g <- '3%'
#Check for aid type,purpose_code,donor_code,pba,short_description and long_description
oda_filter$oda_donor_bundle[which(( oda_filter$purpose_code == purpose_code_v |
oda_filter$pba == pba_v |
as.character(oda_filter$channel_code) %like% channel_code_g) &
oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'B'
# Bundle code D
channel_code_g <- '51%'
#Check for channel code
oda_filter$oda_donor_bundle[which(as.character(oda_filter$channel_code) %like% channel_code_g &
oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'D'
# Bundle Code B
donor_code_v <- c(901,905,906,907,909,912,913,914,915,916,921,951,953,958,976,990,1013)
description_g <- 'finance| fund |subsidy|financement|fonds|subsidie'
swp <- ' SWAP '
space = function(x){
return(gsub('[[:punct:] ]+',' ',paste0(" ",x," "),useBytes=T))
}
oda_filter$short_description_l <- space(oda_filter$short_description)
oda_filter$long_description_l <- space(oda_filter$long_description)
oda_filter$oda_donor_bundle[which(oda_filter$donor_code %in% donor_code_v & oda_filter$usd_disbursement >= 1 &
oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'B'
oda_filter$oda_donor_bundle[which(( grepl(description_g,oda_filter$short_description_l,ignore.case = TRUE, useBytes = TRUE)) &
oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'B'
oda_filter$oda_donor_bundle[which(( grepl(description_g,oda_filter$long_description_l,ignore.case = TRUE, useBytes = TRUE)) &
oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'B'
oda_filter$oda_donor_bundle[which(( grepl(swp,oda_filter$long_description,ignore.case = FALSE, useBytes = TRUE)) &
oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'B'
# Bundle Code D
long_description_g <- 'research'
# grep %research% and recipient code 998
oda_filter$oda_donor_bundle[which(grepl(long_description_g,oda_filter$long_description,ignore.case = TRUE, useBytes = TRUE) & oda_filter$recipient_code == 9998 &
oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'D'
# Bundle G
description_g <- 'consultancy|consultant|technical assistance|technical cooperation|training'
#Search for the occurrences of short_description and long_description
oda_filter$oda_donor_bundle[which((grepl(description_g,oda_filter$long_description_l,ignore.case = TRUE, useBytes = TRUE) |
grepl(description_g,oda_filter$short_description_l,ignore.case = TRUE, useBytes = TRUE)) &
oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'G'
# Bundle Code C
description_g <-'vaccin|non-food|nonfood|non alimentaire'
# Check for purpose code, short_description and long_description
oda_filter$oda_donor_bundle[which( grepl(description_g,oda_filter$long_description_l,ignore.case = TRUE, useBytes = TRUE)
|grepl(description_g,oda_filter$short_description_l,ignore.case = TRUE, useBytes = TRUE)
& oda_filter$oda_donor_bundle %!in% ignore_bundle_code)] <- 'C'
oda_filter[,.('Number of Rows'=.N),by='oda_donor_bundle']
oda_filter$channel_code[which(as.character(oda_filter$channel_code) %like% channel_code_g
& oda_filter$oda_donor_bundle %!in% ignore_bundle_code)]
save(oda_filter, file="output/oda_filter.RData")
# load("output/oda_filter.RData")
names(channel_code_5_web_map)[1] = "channel_code"
channel_code_5_web_map$itep_channel_name = NULL
oda_filter = merge(oda_filter,channel_code_5_web_map, by="channel_code",all.x=T)
names(sector_3_code_map) = c("sector_code","itep_sector_name")
oda_filter = merge(oda_filter,sector_3_code_map, by="sector_code",all.x=T)
oda_tab = oda_filter[,.(
usd_disbursement_deflated=sum(usd_disbursement_deflated,na.rm=T),
usd_disbursement=sum(usd_disbursement,na.rm=T),
usd_commitment_deflated=sum(usd_commitment_deflated,na.rm=T),
usd_commitment=sum(usd_commitment,na.rm=T)
),by=.(
donor_name,
recipient_name,
itep_sector_name,
oda_donor_bundle,
itep_channel_web_id,
year
)]
bundle_map = c("Cash (loans/equity)","Cash grants", "Commodities and food", "GPGs and NNGOs", "Mixed project aid", "Non-transfer", "Technical cooperation")
names(bundle_map) = c("A","B","C","D","E","F","G")
oda_tab$bundle_name = bundle_map[oda_tab$oda_donor_bundle]
oda_tab$oda_donor_bundle = NULL
oda_tab$itep_channel_web_id[which(is.na(oda_tab$itep_channel_web_id))] = "unspecified"
fwrite(oda_tab,"output/bundle_test.csv")