-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode_names.R
More file actions
239 lines (225 loc) · 7.26 KB
/
Copy pathcode_names.R
File metadata and controls
239 lines (225 loc) · 7.26 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#' Get area names from area codes
#'
#' @description
#' Add a new column to an existing tibble with the corresponding name
#' for each code. The codes are assumed to be from those defined by
#' the `FABIO` model.
#'
#' @param table The table that will be modified with a new column.
#' @param code_column The name of the column in `table` containing the codes.
#' @param name_column The name of the output column containing the names.
#'
#' @returns A tibble with all the contents of `table` and an extra column
#' named `name_column`, which contains the names. If there is no name match,
#' an `NA` is included.
#'
#' @export
#'
#' @examples
#' table <- tibble::tibble(area_code = c(1, 2, 4444, 3))
#'
#' add_area_name(table)
#'
#' table |>
#' dplyr::rename(my_area_code = area_code) |>
#' add_area_name(code_column = "my_area_code")
#'
#' add_area_name(table, name_column = "my_custom_name")
add_area_name <- function(
table,
code_column = "area_code",
name_column = "area_name") {
regions <- .get_regions(name_column, code_column)
table |>
dplyr::left_join(regions, {{ code_column }})
}
#' Get area codes from area names
#'
#' @description
#' Add a new column to an existing tibble with the corresponding code
#' for each name. The codes are assumed to be from those defined by
#' the `FABIO` model.
#'
#' @param table The table that will be modified with a new column.
#' @param code_column The name of the output column containing the codes.
#' @param name_column The name of the column in `table` containing the names.
#'
#' @returns A tibble with all the contents of `table` and an extra column
#' named `code_column`, which contains the codes. If there is no code match,
#' an `NA` is included.
#'
#' @export
#'
#' @examples
#' table <- tibble::tibble(
#' area_name = c("Armenia", "Afghanistan", "Dummy Country", "Albania")
#' )
#'
#' add_area_code(table)
#'
#' table |>
#' dplyr::rename(my_area_name = area_name) |>
#' add_area_code(name_column = "my_area_name")
#'
#' add_area_code(table, code_column = "my_custom_code")
add_area_code <- function(
table,
name_column = "area_name",
code_column = "area_code") {
regions <- .get_regions(name_column, code_column)
table |>
dplyr::left_join(regions, {{ name_column }})
}
#' Get item names from item codes
#'
#' @description
#' Add a new column to an existing tibble with the corresponding name
#' for each item code. The codes are assumed to be from those defined by
#' FAOSTAT.
#'
#' @param table The table that will be modified with a new column.
#' @param code_column The name of the column in `table` containing the codes.
#' @param name_column The name of the output column containing the names.
#'
#' @returns A tibble with all the contents of `table` and an extra column
#' named `name_column`, which contains the names. If there is no name match,
#' an `NA` is included.
#'
#' @export
#'
#' @examples
#' table <- tibble::tibble(item_code = c(2559, 2744, 9876))
#' add_item_name(table)
#'
#' table |>
#' dplyr::rename(my_item_code = item_code) |>
#' add_item_name(code_column = "my_item_code")
#'
#' add_item_name(table, name_column = "my_custom_name")
add_item_name <- function(
table,
code_column = "item_code",
name_column = "item_name") {
items <- .get_items(name_column, code_column)
table |>
dplyr::left_join(items, {{ code_column }})
}
#' Get item codes from item names
#'
#' @description
#' Add a new column to an existing tibble with the corresponding code
#' for each item name. The codes are assumed to be from those defined by
#' the FAOSTAT.
#'
#' @param table The table that will be modified with a new column.
#' @param code_column The name of the output column containing the codes.
#' @param name_column The name of the column in `table` containing the names.
#'
#' @returns A tibble with all the contents of `table` and an extra column
#' named `code_column`, which contains the codes. If there is no code match,
#' an `NA` is included.
#'
#' @export
#'
#' @examples
#' table <- tibble::tibble(item_name = c("Cottonseed", "Eggs", "Dummy Item"))
#' add_item_code(table)
#'
#' table |>
#' dplyr::rename(my_item_name = item_name) |>
#' add_item_code(name_column = "my_item_name")
#'
#' add_item_code(table, code_column = "my_custom_code")
add_item_code <- function(
table,
name_column = "item_name",
code_column = "item_code") {
items <- .get_items(name_column, code_column)
table |>
dplyr::left_join(items, {{ name_column }})
}
#' Get process names from process codes
#'
#' @description
#' Add a new column to an existing tibble with the corresponding name
#' for each process code. The codes are assumed to be from those defined by
#' the FABIO model.
#'
#' @param table The table that will be modified with a new column.
#' @param code_column The name of the column in `table` containing the codes.
#' @param name_column The name of the output column containing the names.
#'
#' @returns A tibble with all the contents of `table` and an extra column
#' named `name_column`, which contains the names. If there is no name match,
#' an `NA` is included.
#'
#' @export
#'
#' @examples
#' table <- tibble::tibble(process_code = c("p017", "p076", "dummy"))
#' add_process_name(table)
#'
#' table |>
#' dplyr::rename(my_process_code = process_code) |>
#' add_process_name(code_column = "my_process_code")
#'
#' add_process_name(table, name_column = "my_custom_name")
add_process_name <- function(
table,
code_column = "process_code",
name_column = "process_name") {
processes <- .get_processes(name_column, code_column)
table |>
dplyr::left_join(processes, {{ code_column }})
}
#' Get process codes from process names
#'
#' @description
#' Add a new column to an existing tibble with the corresponding code
#' for each process name. The codes are assumed to be from those defined by
#' the FABIO model.
#'
#' @param table The table that will be modified with a new column.
#' @param code_column The name of the output column containing the codes.
#' @param name_column The name of the column in `table` containing the names.
#'
#' @returns A tibble with all the contents of `table` and an extra column
#' named `code_column`, which contains the codes. If there is no code match,
#' an `NA` is included.
#'
#' @export
#'
#' @examples
#' table <- tibble::tibble(
#' process_name = c("Beans production", "Olive Oil extraction", "Dummy")
#' )
#' add_process_code(table)
#'
#' table |>
#' dplyr::rename(my_process_name = process_name) |>
#' add_process_code(name_column = "my_process_name")
#'
#' add_process_code(table, code_column = "my_custom_code")
add_process_code <- function(
table,
name_column = "process_name",
code_column = "process_code") {
processes <- .get_processes(name_column, code_column)
table |>
dplyr::left_join(processes, {{ name_column }})
}
.get_regions <- function(name_column, code_column) {
"input/raw/regions.csv" |>
.read_local_csv() |>
dplyr::select(!!name_column := area, !!code_column := area_code)
}
.get_items <- function(name_column, code_column) {
"input/raw/items.csv" |>
.read_local_csv() |>
dplyr::select(!!name_column := item, !!code_column := item_code)
}
.get_processes <- function(name_column, code_column) {
"input/raw/processes.csv" |>
.read_local_csv() |>
dplyr::select(!!name_column := process, !!code_column := process_code)
}