-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_thesauruses.R
177 lines (156 loc) · 6.38 KB
/
07_thesauruses.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
#### STEP 1 Main Variable table ####
## The main theasurus is the VARIABLES_THESAURUS, which will contain all the variables,
## their old names, the translations, the scenarios in which they are involved, their
## type (chr, num...), their descriptions in each lenguage, their presence in the
## different versions...
# lets obtain the variable names (as var_id) and the class (as var_type) for each table
# and join them all together, to remove the repeated ones later on
## helper function to avoid c&p a lot
table_and_type <- function(x) {
rlang::sym(x) %>%
rlang::eval_tidy() %>%
summarise_all(~class(.x)[1]) %>%
gather(var_id, var_type) %>%
mutate(var_table = x) %>%
select(var_id, var_table, var_type)
}
tables_names <- c(
'PLOTS', 'PLOTS_NFI_2_DYNAMIC_INFO', 'PLOTS_NFI_3_DYNAMIC_INFO', 'PLOTS_NFI_4_DYNAMIC_INFO',
'PLOT_NFI_2_RESULTS', 'PLOT_NFI_2_DIAMCLASS_RESULTS',
'PLOT_NFI_3_RESULTS','PLOT_NFI_3_DIAMCLASS_RESULTS',
'PLOT_NFI_4_RESULTS', 'PLOT_NFI_4_DIAMCLASS_RESULTS',
'SPECIES_NFI_2_RESULTS', 'SPECIES_NFI_2_DIAMCLASS_RESULTS',
'SPECIES_NFI_3_RESULTS', 'SPECIES_NFI_3_DIAMCLASS_RESULTS',
'SPECIES_NFI_4_RESULTS', 'SPECIES_NFI_4_DIAMCLASS_RESULTS',
'SIMPSPECIES_NFI_2_RESULTS', 'SIMPSPECIES_NFI_2_DIAMCLASS_RESULTS',
'SIMPSPECIES_NFI_3_RESULTS', 'SIMPSPECIES_NFI_3_DIAMCLASS_RESULTS',
'SIMPSPECIES_NFI_4_RESULTS', 'SIMPSPECIES_NFI_4_DIAMCLASS_RESULTS',
'GENUS_NFI_2_RESULTS', 'GENUS_NFI_2_DIAMCLASS_RESULTS',
'GENUS_NFI_3_RESULTS', 'GENUS_NFI_3_DIAMCLASS_RESULTS',
'GENUS_NFI_4_RESULTS', 'GENUS_NFI_4_DIAMCLASS_RESULTS',
'BC_NFI_2_RESULTS', 'BC_NFI_2_DIAMCLASS_RESULTS',
'BC_NFI_3_RESULTS', 'BC_NFI_3_DIAMCLASS_RESULTS',
'BC_NFI_4_RESULTS', 'BC_NFI_4_DIAMCLASS_RESULTS',
'DEC_NFI_2_RESULTS', 'DEC_NFI_2_DIAMCLASS_RESULTS',
'DEC_NFI_3_RESULTS', 'DEC_NFI_3_DIAMCLASS_RESULTS',
'DEC_NFI_4_RESULTS', 'DEC_NFI_4_DIAMCLASS_RESULTS',
'PLOT_COMP_NFI2_NFI3_RESULTS', 'PLOT_COMP_NFI2_NFI3_DIAMCLASS_RESULTS',
'PLOT_COMP_NFI3_NFI4_RESULTS', 'PLOT_COMP_NFI3_NFI4_DIAMCLASS_RESULTS',
'SPECIES_COMP_NFI2_NFI3_RESULTS', 'SPECIES_COMP_NFI2_NFI3_DIAMCLASS_RESULTS',
'SPECIES_COMP_NFI3_NFI4_RESULTS', 'SPECIES_COMP_NFI3_NFI4_DIAMCLASS_RESULTS',
'SIMPSPECIES_COMP_NFI2_NFI3_RESULTS', 'SIMPSPECIES_COMP_NFI2_NFI3_DIAMCLASS_RESULTS',
'SIMPSPECIES_COMP_NFI3_NFI4_RESULTS', 'SIMPSPECIES_COMP_NFI3_NFI4_DIAMCLASS_RESULTS',
'GENUS_COMP_NFI2_NFI3_RESULTS', 'GENUS_COMP_NFI2_NFI3_DIAMCLASS_RESULTS',
'GENUS_COMP_NFI3_NFI4_RESULTS', 'GENUS_COMP_NFI3_NFI4_DIAMCLASS_RESULTS',
'DEC_COMP_NFI2_NFI3_RESULTS', 'DEC_COMP_NFI2_NFI3_DIAMCLASS_RESULTS',
'DEC_COMP_NFI3_NFI4_RESULTS', 'DEC_COMP_NFI3_NFI4_DIAMCLASS_RESULTS',
'BC_COMP_NFI2_NFI3_RESULTS', 'BC_COMP_NFI2_NFI3_DIAMCLASS_RESULTS',
'BC_COMP_NFI3_NFI4_RESULTS', 'BC_COMP_NFI3_NFI4_DIAMCLASS_RESULTS',
'SHRUB_NFI_2_INFO', 'SHRUB_NFI_3_INFO', 'SHRUB_NFI_4_INFO',
'REGENERATION_NFI_2', 'REGENERATION_NFI_3', 'REGENERATION_NFI_4'
)
tables_names %>%
purrr::map(table_and_type) %>%
bind_rows() %>%
mutate(
presence_scenario1 = case_when(
var_table %in% {
tables_names %>% magrittr::extract(stringr::str_detect(tables_names, 'PLOT'))
} ~ TRUE,
TRUE ~ FALSE
),
presence_scenario2 = case_when(
var_table %in% {
tables_names %>% magrittr::extract(!stringr::str_detect(tables_names, '^PLOT_'))
} ~ TRUE,
TRUE ~ FALSE
),
presence_scenario3 = presence_scenario1,
presence_scenario4 = presence_scenario2,
## empty variables to contain the translations, to be filled by hand
translation_spa = '',
translation_cat = '',
translation_eng = '',
var_description_spa = '',
var_description_cat = '',
var_description_eng = ''
) -> vars_table
## now, the translations must be done in a collaborative document. so we save this as an
## excel file
writexl::write_xlsx(vars_table, 'data_raw/variables_thesaurus.xlsx')
## now we create the derived thesaurus from the variables_thesaurus:
#### STEP 2 By type thesauruses ####
# categorial
categorical_variables <- vars_table %>%
filter(var_type == 'character') %>%
select(var_id, var_table)
## little helper of santa categorical
categorical_values <- function(x) {
rlang::sym(x) %>%
rlang::eval_tidy() %>%
select(one_of(categorical_variables %>% pull(var_id) %>% unique())) %>%
gather('var_id', 'var_values') %>%
distinct() %>%
nest(-var_id, .key = 'var_values') %>%
mutate(var_values = map(var_values, pull), var_table = x)
}
categorical_variables <- tables_names %>%
purrr::map(categorical_values) %>%
bind_rows %>%
right_join(categorical_variables, by = c('var_id', 'var_table')) %>%
# no easy manage of arrays exists yet between postgres and r so, let's unnest and create
# a key, that in combination with var_id is unique
unnest() %>%
mutate(
dummy_id = 1:nrow(.),
var_table = tolower(var_table)
) %>%
select(dummy_id, var_id, var_table, var_values)
## numerical
numerical_variables <- vars_table %>%
filter(var_type %in% c('numeric', 'integer')) %>%
select(var_id, var_table) %>%
mutate(var_units = '')
# little helper of santa numerical
numerical_values <- function(x) {
rlang::sym(x) %>%
rlang::eval_tidy() %>%
select(one_of(numerical_variables %>% pull(var_id) %>% unique())) %>%
summarise_all(
.funs = dplyr::funs(
min = floor(min(., na.rm = TRUE)), max = ceiling(max(., na.rm = TRUE))
)
) -> min_max
min_max %>%
select(ends_with('_min')) %>%
magrittr::set_names(
., stringr::str_replace(names(.), '_min$', '')
) %>%
tidyr::gather('var_id', 'var_min') %>%
dplyr::full_join(
min_max %>%
select(ends_with('_max')) %>%
magrittr::set_names(
., stringr::str_replace(names(.), '_max$', '')
) %>%
tidyr::gather('var_id', 'var_max'),
by = 'var_id'
) %>%
mutate(var_table = x)
}
numerical_variables <- tables_names %>%
purrr::map(numerical_values) %>%
bind_rows() %>%
right_join(numerical_variables, by = c('var_id', 'var_table')) %>%
select(var_id, var_table, everything()) %>%
mutate(var_table = tolower(var_table))
writexl::write_xlsx(numerical_variables, 'data_raw/numerical_variables.xlsx')
# logical
logical_variables <- vars_table %>%
filter(var_type == 'logical') %>%
select(var_id)
# dates
dttm_variables <- vars_table %>%
filter(var_type == 'POSIXct') %>%
select(var_id, var_table) %>%
mutate(var_table = tolower(var_table))