-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoneyPerDisciplinePerYear.R
285 lines (201 loc) · 7.08 KB
/
MoneyPerDisciplinePerYear.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
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
##### Data for the animation ######
### this one is a little tricky, as an illusion of gradual increases/decreases had to be created
if(!require(pacman)){
install.packages(
"pacman",
install = TRUE
)
}
pacman :: p_load(
tidyverse,
rio,
janitor,
lubridate,
jsonlite,
install = TRUE
)
dir.create("./input/")
dir.create("./output/")
fileList = list.files("./input/", full.names = TRUE)
#### Importing files & processing them
pubEx = read_csv2("http://p3.snf.ch/P3Export/P3_PublicationExport.csv") %>%
set_names(., str_replace_all(names(.), "\\s", "_"))
# pubEx = read_csv2("./input/P3_PublicationExport.csv") %>%
# set_names(., str_replace_all(names(.), "\\s", "_"))
pubEx = pubEx %>%
select(Publication_ID_SNSF, Project_Number, Publication_Year, Open_Access_Status, Open_Access_Type, Authors, Title_of_Publication) %>%
distinct(Title_of_Publication, .keep_all = TRUE)
####### the grants ##########
### here we need
graEx = read_csv2("http://p3.snf.ch/P3Export/P3_GrantExport.csv") %>%
set_names(., str_replace_all(names(.), "\\s", "_"))
graEx = graEx %>%
select(-Project_Number_String, -Project_Title, -Project_Title_English, -Funding_Instrument_Hierarchy, -Keywords) %>% ## cut out some data which is not needed
mutate(
Approved_Amount = as.numeric(Approved_Amount), ### convert from string to number
End_Date = dmy(End_Date), ### parse date
Start_Date = dmy(Start_Date), ### parse another date
Start_Year = format(Start_Date, "%Y") %>% as.numeric(), ###...and another
End_Year = format(End_Date, "%Y") %>% as.numeric(), ### ...and yet another
durationYears = End_Year - Start_Year, ### we want to calculate the durations to allocate the money to each year
durationYears = case_when(
durationYears == 0 ~ 1, ## we treat each project with a duration of 0 years as one with 1 year, as it cannot have a duration of 0 years
TRUE ~ durationYears
),
yearlyAmount = Approved_Amount / durationYears ### get the yearly amount for each project
)
### check for duplicates so we are not counting money twice!
graEx %>%
filter(duplicated(Project_Number))
#### I want to calculate the yearly money per project - I have to get durations of the projects and extend the data set this way
durations = graEx %>%
select(
Project_Number, Start_Year, End_Year, durationYears
) %>%
filter(
!is.na(Start_Year) | !is.na(End_Year)
)
### this gives me the start and the end as well as the project number with the amount of years a project presumably took.
### WARNING: This loop will take a while
durationData = NULL
##########
### this loop will compile for each project a sequence of years the project was running
# for(i in durations$Project_Number){
#
#
# print(i)
#
#
# durationSlice = durations %>% filter(
# Project_Number == i
# ) %>%
# mutate(
# durationYears = durationYears + 1
# )
#
# start = durationSlice$Start_Year
# end = durationSlice$End_Year
# durYr = durationSlice$durationYears
#
#
#
#
#
# yearSeq = seq(start, end, by = 1) %>%
# data.frame(duration_sequence = .)
#
# # print(yearSeq)
#
# durationYears = durationSlice$durationYears
# durationYears = durationYears
#
# # print(durationYears)
#
# sliceTemp = durationSlice %>% slice(rep(1:n(), each = durationYears))
#
# if(unique(sliceTemp$Start_Year) == unique(sliceTemp$End_Year)){5
#
# sliceTemp = sliceTemp %>%
# distinct() %>%
# mutate(
# durationYears = 1
# )
#
# }
#
# sliceTemp = sliceTemp %>%
# data_frame(., yearSeq)
#
#
# # print(sliceTemp)
#
# durationData = durationData %>%
# rbind(., sliceTemp)
#
#
# }
##############
#### load the existing data file, instead of doing a 20 mins loop
load("durationsData.RData")### we import it here, as it takes quite long - yes one could parallelise it, I know ;-)
durationData = durationData %>%
select(Project_Number, duration_sequence)### each project with the respective duration
#### join the durations with the grant data
temp = graEx %>%
select(Project_Number, Approved_Amount, Discipline_Name, durationYears) %>%
left_join(durationData, ., by = "Project_Number") %>%
# left_join(., OARatio, by = "Project_Number") %>%
mutate(
yearlyAmount = Approved_Amount / durationYears
)
### Aggregating data
joinedDat = graEx %>%
left_join(., pubEx, by = "Project_Number") %>%
mutate(
Project_Number = as.character(Project_Number)
) %>%
distinct() #%>%
### the Money per discipline is not used in the viz, but I'll keep it here
disciplineMoney = joinedDat %>%
group_by(Discipline_Name) %>%
summarise(
amountPerDisc = sum(Approved_Amount, na.rm = TRUE)
) %>%
toJSON(., pretty = TRUE) %>%
write(., "./oaData/DisciplineAmounts.json")
### here the real magic happens
### we want to produce an effect, to see a gradual increase. For this reason we need to split the data up into tenths of years with the money also divided by ten
disciplineYearMoney = temp %>%
group_by(
Discipline_Name, duration_sequence
) %>%
summarise(
amountPerDiscYear = sum(yearlyAmount)### get the money per discipline per year
) %>%
ungroup() %>%
rename(
name = Discipline_Name,
year = duration_sequence,
value = amountPerDiscYear### name it handily for the plot
) %>%
group_by(year) %>%
mutate(
Rank = dense_rank(value),
value = value / 10^3### get it into 1k CHF
) %>% ungroup() %>%
arrange(name, year) %>%
group_by(name) %>%
mutate(
lastValue = lag(value), ###as the application needs a value to reference to which was a value before the actual value, this lag has to be created
lastValue = case_when(
is.na(lastValue) ~ 0,###if the last value is NA, it's 0, otherwise it gets choppy
TRUE ~ lastValue
)
) %>%
mutate(#### simulating a linear gradient
difference = value - lastValue,
differenceTenth = difference / 10,### s
yearTenth = 0.1
) %>%
group_by(name) %>%
slice(rep(seq_len(n()), each = 10)) %>% ### stretch the data, so each tenth of a year is represented
ungroup() %>%
group_by(name, year) %>%
mutate(
yearTenth = cumsum(yearTenth) - yearTenth, ### cumulative sum for each tenth per year and per discipline
differenceTenth = cumsum(differenceTenth) - differenceTenth,
valueOld = value,
value = differenceTenth + lastValue
) %>%
ungroup() %>%
mutate(
year = year + yearTenth
) %>%
group_by(name) %>%
mutate(
lastValueOld = lastValue,
lastValue = lag(value),
lastValue = replace_na(lastValue, 0)
) %>%
select(year, name, value, lastValue, Rank) %>%
toJSON(., pretty = TRUE) %>%
write(., "./oaData/DisciplineAmountsPerYear.json")### parse the json and have fun