-
Notifications
You must be signed in to change notification settings - Fork 0
/
Writer_Entrepr_data.R
64 lines (55 loc) · 2.56 KB
/
Writer_Entrepr_data.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
# ------------------------
# Data pull from TCdata360 API
# ------------------------
library(jsonlite)
library(tidyverse)
# Query data based on ids of filtered indicators
# loop by country and indicator id. Bind it all in a data.frame
# Query country metadata:
countries <- fromJSON("http://datascope-prod.amida-demo.com/api/v1/countries/?fields=id%2Ciso2%2Ciso3%2Cname%2Cregion%2CincomeLevel%2ClendingType%2CcapitalCity%2Cgeo",
flatten = TRUE)
# Query indicators:
indicators <- fromJSON("http://datascope-prod.amida-demo.com/api/v1/indicators?fields=id%2Cname%2Cdataset%2CvalueType%2CdatasetId%2Cnotes%2Cproperties%2Crank%2Cdefinition",
flatten=TRUE)
# Read data description file (what goes in the PDF report)
dataDesc <- read.csv("Entrepr_DataDescription.csv", stringsAsFactors = FALSE)
indicatorsID <- filter(dataDesc, !is.na(tcdata360_id))$tcdata360_id
indicators_Entrep <- indicators %>%
filter(id %in% indicatorsID) %>%
distinct(id,.keep_all=TRUE) %>%
arrange(name)
Entrepr_data <- data.frame()
specialchars <- paste(c("[-]","[.]"),collapse = "|")
#for (cou in c("BRA","ARG","ECU","URY","BOL")){
for (cou in countries$id){
for (ind in indicators_Entrep$id){
print(paste0("Processing...",cou," ",ind))
thisQuery <- fromJSON(paste0("http://datascope-prod.amida-demo.com/api/v1/data?countries=",cou,
"&indicators=",ind),
flatten = TRUE)
if (length(thisQuery$data)>0){
thisQuery <- flatten(thisQuery$data$indicators[[1]])
if (!is.null(thisQuery$estimated)){
thisQuery$estimated <- NULL
thisQuery <- as.data.frame(thisQuery)
}
thisQuery <- thisQuery %>%
mutate(iso3 = cou)
names(thisQuery) <- gsub("values.","",names(thisQuery),fixed=TRUE)
names(thisQuery) <- ifelse(grepl(specialchars,names(thisQuery)),substr(names(thisQuery),1,4),names(thisQuery))
# consolidate quarterly data by the 4th quarter
names(thisQuery) <- gsub("Q4","",names(thisQuery))
thisQuery <- select(thisQuery, -dplyr::contains("Q"))
if (nrow(Entrepr_data)==0) {
Entrepr_data <- thisQuery
} else {
Entrepr_data <- bind_rows(Entrepr_data,thisQuery)
}
}
}
}
# create Period variable
Entrepr_data <- gather(Entrepr_data, Period, Observation, -iso3,-id)
# -----------------------------------------------------------
write.csv(Entrepr_data,"/Users/asanchez3/Desktop/Work/TCMN/Entrepreneurship_data/Entrepr_data.csv",row.names = FALSE)
# -----------------------------------------------------------