-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtidying_things_up_bitches.R
356 lines (304 loc) · 13.3 KB
/
tidying_things_up_bitches.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
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
library(devtools)
library(tidyverse)
library(future)
install_github("KTH-Library/swecris", dependencies = TRUE)
library(swecris)
################################################################################
#### I modified the following function from swecris, and removed the search string that specified KTH
#kth_projects <- swecris_funding()
swecris_all_university_projects <- function (searchstring = "",
token)
{
if (missing(token))
token <- "RWNDZ3FDRVVSMmNUNlZkMkN3"
httr::GET("https://swecris-api.vr.se/v1/scp/export", query = list(`organizationType[]` = "Universitet",
sortOrder = "desc", sortColumn = "FundingStartDate",
searchText = URLencode(searchstring), token = token)) %>%
httr::content(as = "text", encoding = "UTF-8") %>% readr::read_delim(delim = ";",
quote = "\"", show_col_types = FALSE)
}
#### get the data to an objectc()
all_university_projects <- swecris_all_university_projects()
#### get new dataframe only with the involved people and proj id
all_university_projects_people <- all_university_projects %>%
select(ProjectId, InvolvedPeople)
#### check number of cores
unname(availableCores())
#### parallel pipeline using 6 cores
t1 = proc.time()
plan(multisession)
a1 %<-% {
n_times = 1
inv.people <- tibble()
for (i in all_university_projects_people$InvolvedPeople[1:10000]) {
temp.inv.people <- tibble(swecris::parse_involved_people(i)) %>%
add_column(.data = data.frame(ProjectId = rep(all_university_projects_people$ProjectId[n_times],
times = nrow(swecris::parse_involved_people(i)))))
inv.people <- bind_rows(inv.people, temp.inv.people)
n_times = n_times + 1
}
inv.people
} %seed% 42 # Use the %seed% to specify the same seed for all of them, for random number gen
a2 %<-% {
n_times = 1
inv.people <- tibble()
for (i in all_university_projects_people$InvolvedPeople[10001:20000]) {
temp.inv.people <- tibble(swecris::parse_involved_people(i)) %>%
add_column(.data = data.frame(ProjectId = rep(all_university_projects_people$ProjectId[n_times],
times = nrow(swecris::parse_involved_people(i)))))
inv.people <- bind_rows(inv.people, temp.inv.people)
n_times = n_times + 1
}
inv.people
} %seed% 42 # Use the %seed% to specify the same seed for all of them, for random number gen
a3 %<-% {
n_times = 1
inv.people <- tibble()
for (i in all_university_projects_people$InvolvedPeople[20001:30000]) {
temp.inv.people <- tibble(swecris::parse_involved_people(i)) %>%
add_column(.data = data.frame(ProjectId = rep(all_university_projects_people$ProjectId[n_times],
times = nrow(swecris::parse_involved_people(i)))))
inv.people <- bind_rows(inv.people, temp.inv.people)
n_times = n_times + 1
}
inv.people
} %seed% 42 # Use the %seed% to specify the same seed for all of them, for random number gen
a4 %<-% {
n_times = 1
inv.people <- tibble()
for (i in all_university_projects_people$InvolvedPeople[30001:40000]) {
temp.inv.people <- tibble(swecris::parse_involved_people(i)) %>%
add_column(.data = data.frame(ProjectId = rep(all_university_projects_people$ProjectId[n_times],
times = nrow(swecris::parse_involved_people(i)))))
inv.people <- bind_rows(inv.people, temp.inv.people)
n_times = n_times + 1
}
inv.people
} %seed% 42 # Use the %seed% to specify the same seed for all of them, for random number gen
a5 %<-% {
n_times = 1
inv.people <- tibble()
for (i in all_university_projects_people$InvolvedPeople[40001:50000]) {
temp.inv.people <- tibble(swecris::parse_involved_people(i)) %>%
add_column(.data = data.frame(ProjectId = rep(all_university_projects_people$ProjectId[n_times],
times = nrow(swecris::parse_involved_people(i)))))
inv.people <- bind_rows(inv.people, temp.inv.people)
n_times = n_times + 1
}
inv.people
} %seed% 42 # Use the %seed% to specify the same seed for all of them, for random number gen
a6 %<-% {
n_times = 1
inv.people <- tibble()
for (i in all_university_projects_people$InvolvedPeople[50001:55318]) {
temp.inv.people <- tibble(swecris::parse_involved_people(i)) %>%
add_column(.data = data.frame(ProjectId = rep(all_university_projects_people$ProjectId[n_times],
times = nrow(swecris::parse_involved_people(i)))))
inv.people <- bind_rows(inv.people, temp.inv.people)
n_times = n_times + 1
}
inv.people
} %seed% 42 # Use the %seed% to specify the same seed for all of them, for random number gen
involved.people <- bind_rows(a1, a2, a3, a4, a5, a6)
t2 = proc.time()
t2 - t1
write_csv2(involved.people, "involved_people_projID.csv")
################################################################################
# get new dataframe only with the Scbs and proj id
all_university_projects_scbs <- all_university_projects %>%
select(ProjectId, Scbs)
#### parallel pipeline using 6 cores
t1 = proc.time()
plan(multisession)
a1 %<-% {
n_times = 1
scbs.codes <- tibble()
for (i in all_university_projects_scbs$Scbs[1:10000]) {
temp.scbs.codes <- tibble(swecris::parse_scb_codes(i)) %>%
add_column(.data = data.frame(ProjectId = rep(all_university_projects_scbs$ProjectId[n_times],
times = nrow(swecris::parse_scb_codes(i)))))
scbs.codes <- bind_rows(scbs.codes, temp.scbs.codes)
n_times = n_times + 1
}
scbs.codes
} %seed% 42 # Use the %seed% to specify the same seed for all of them, for random number gen
a2 %<-% {
n_times = 1
scbs.codes <- tibble()
for (i in all_university_projects_scbs$Scbs[10001:20000]) {
temp.scbs.codes <- tibble(swecris::parse_scb_codes(i)) %>%
add_column(.data = data.frame(ProjectId = rep(all_university_projects_scbs$ProjectId[n_times],
times = nrow(swecris::parse_scb_codes(i)))))
scbs.codes <- bind_rows(scbs.codes, temp.scbs.codes)
n_times = n_times + 1
}
scbs.codes
} %seed% 42 # Use the %seed% to specify the same seed for all of them, for random number gen
a3 %<-% {
n_times = 1
scbs.codes <- tibble()
for (i in all_university_projects_scbs$Scbs[20001:30000]) {
temp.scbs.codes <- tibble(swecris::parse_scb_codes(i)) %>%
add_column(.data = data.frame(ProjectId = rep(all_university_projects_scbs$ProjectId[n_times],
times = nrow(swecris::parse_scb_codes(i)))))
scbs.codes <- bind_rows(scbs.codes, temp.scbs.codes)
n_times = n_times + 1
}
scbs.codes
} %seed% 42 # Use the %seed% to specify the same seed for all of them, for random number gen
a4 %<-% {
n_times = 1
scbs.codes <- tibble()
for (i in all_university_projects_scbs$Scbs[30001:40000]) {
temp.scbs.codes <- tibble(swecris::parse_scb_codes(i)) %>%
add_column(.data = data.frame(ProjectId = rep(all_university_projects_scbs$ProjectId[n_times],
times = nrow(swecris::parse_scb_codes(i)))))
scbs.codes <- bind_rows(scbs.codes, temp.scbs.codes)
n_times = n_times + 1
}
scbs.codes
} %seed% 42 # Use the %seed% to specify the same seed for all of them, for random number gen
a5 %<-% {
n_times = 1
scbs.codes <- tibble()
for (i in all_university_projects_scbs$Scbs[40001:50000]) {
temp.scbs.codes <- tibble(swecris::parse_scb_codes(i)) %>%
add_column(.data = data.frame(ProjectId = rep(all_university_projects_scbs$ProjectId[n_times],
times = nrow(swecris::parse_scb_codes(i)))))
scbs.codes <- bind_rows(scbs.codes, temp.scbs.codes)
n_times = n_times + 1
}
scbs.codes
} %seed% 42 # Use the %seed% to specify the same seed for all of them, for random number gen
a6 %<-% {
n_times = 1
scbs.codes <- tibble()
for (i in all_university_projects_scbs$Scbs[50001:55318]) {
temp.scbs.codes <- tibble(swecris::parse_scb_codes(i)) %>%
add_column(.data = data.frame(ProjectId = rep(all_university_projects_scbs$ProjectId[n_times],
times = nrow(swecris::parse_scb_codes(i)))))
scbs.codes <- bind_rows(scbs.codes, temp.scbs.codes)
n_times = n_times + 1
}
scbs.codes
} %seed% 42 # Use the %seed% to specify the same seed for all of them, for random number gen
scbs.codes <- bind_rows(a1, a2, a3, a4, a5, a6)
t2 = proc.time()
t2 - t1
write_csv2(scbs.codes, "scbs_codes_projID.csv")
grouped.scbs.codes <- group_by(scbs.codes, ProjectId)
################################################################################
#### list containing the universities per county and county code
lan_data <- list(
`01 Stockholms län` = c(
"Stockholms universitet",
"KTH, Kungliga tekniska högskolan",
"Karolinska Institutet",
"Handelshögskolan i Stockholm",
"Södertörns högskola"
),
`03 Uppsala län` = c(
"Uppsala universitet",
"Sveriges lantbruksuniversitet"
),
`04 Södermanlands län` = c(
"Mälardalens högskola"
),
`05 Östergötlands län` = c(
"Linköpings universitet"
),
`06 Jönköpings län` = c(
"Högskolan i Jönköping"
),
`07 Kronobergs län` = c(
"Linnéuniversitetet"
),
`08 Kalmar län` = c(
"Linnéuniversitetet",
"Högskolan i Kalmar"
),
`09 Gotlands län` = c(
"Uppsala universitet"
),
`10 Blekinge län` = c(
"Blekinge Tekniska Högskola"
),
`12 Skåne län` = c(
"Lunds universitet",
"Malmö universitet"
),
`13 Hallands län` = c(
"Högskolan i Halmstad"
),
`14 Västra Götalands län` = c(
"Göteborgs universitet",
"Chalmers tekniska högskola"
),
`17 Värmlands län` = c(
"Karlstads universitet"
),
`18 Örebro län` = c(
"Örebro universitet"
),
`19 Västmanlands län` = c(
"Mälardalens Högskola"
),
`20 Dalarnas län` = c(
"Högskolan Dalarna"
),
`21 Gävleborgs län` = c(
"Högskolan i Gävle"
),
`22 Västernorrlands län` = c(
"Mittuniversitetet"
),
`23 Jämtlands län` = c(
"Mittuniversitetet"
),
`24 Västerbottens län` = c(
"Umeå universitet"
),
`25 Norrbottens län` = c(
"Luleå Tekniska Universitet",
"Umeå universitet"
)
)
#### get/summarize yearly funding per university and keep county code
#### Get the listed data to a tibble with a row per university, maintaining the county and county code.
library(tidyverse)
tibble_data <- enframe(lan_data, name = "County", value = "University")
print(tibble_data)
tibble_data <- tibble_data %>%
mutate(Code = substr(County, 1, 2))
unnested_tibble_data <- tibble_data %>%
rowwise() %>%
do(data.frame(Code = .$Code,
County = paste(str_split(.$County, ' ')[[1]][2:length(str_split(.$County, ' ')[[1]])], collapse = " "),
University = unlist(.$University)))
#### Fetch yearly funding information from master database and merge with our tibble that contains the universities
#### we are interested into
all_university_funding_yearly <- all_university_projects %>%
group_by(CoordinatingOrganisationNameSv, FundingYear) %>%
summarize(
yearly_funding_sek = sum(FundingsSek)
) %>% rename(University = CoordinatingOrganisationNameSv)
university_yearly_sek_data <- left_join(unnested_tibble_data,
all_university_funding_yearly,
by = "University",
relationship = "many-to-many")
################################################################################
#### get/summarize yearly funding per university and scbs code and keep county code
### merge the data frame with the unlsited scbs codes with the master dataframe to have funding info and stuff
university_sek_data_scbs <- left_join(grouped.scbs.codes, all_university_projects, by = "ProjectId") %>%
select(ProjectId, CoordinatingOrganisationNameSv, scb_code, scb_sv_en, FundingYear, FundingsSek) %>%
distinct(scb_code, .keep_all = T) # filter out repeated rows per projectID (some has 6 multiple timpes e.g)
### get yearly funding per scbs code and get info to a df with the universities we are interested into
university_yearly_sek_data_scbs <- university_sek_data_scbs %>%
group_by(CoordinatingOrganisationNameSv, FundingYear, scb_sv_en, scb_code) %>%
summarize(
yearly_funding_sek = sum(FundingsSek)
) %>% rename(University = CoordinatingOrganisationNameSv)
university_yearly_sek_data_scbs <- left_join(unnested_tibble_data,
university_yearly_sek_data_scbs,
by = "University",
relationship = "many-to-many")