-
Notifications
You must be signed in to change notification settings - Fork 3
/
regioncode.R
291 lines (253 loc) · 11.1 KB
/
regioncode.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
#' regioncode
#'
#' `regioncode` is developed to conquer the difficulties to convert various region names and administration division codes of Chinese regions. In the current version, `regioncode` enables seamlessly converting Chinese regions' formal names, common-used names, and geocodes between each other at the prefectural level from 1986 to 2019.
#'
#' @param data_input A character vector for names or a six-digit integer vector for division codes to convert.
#' @param year_from A integer to define the year of the input. The default value is 1999.
#' @param year_to A integer to define the year to convert. The default value is 2015.
#' @param convert_to A character indicating the converting methods. At the prefectural level, valid methods include converting between codes in different years, from codes to city ranks, from codes to region names, from region names to city ranks, from region names to division codes, from region names or division codes to sociopolitical area names, and between names in different years. The current version automatically detect the type of the input. Users only need to choose the output to be codes (`code`), names (`name`) , area (`area`) or city ranks (`rank`). The default option is `code`.
#' When `province` is TRUE, one can also choose `abbre`, `abbreTocode`, `abbreToname`, and `abbreToarea` to convert between names/codes and abbreviations of provinces.
#' @param incomplete_name A logic strong to indicate if the input has incomplete names (not nickname). See more in "Details".
#' @param zhixiashi A logic string to indicate whether treating division codes and names of municipality directly under the central government (Only makes a difference for prefectural-level conversion). The default value is FALSE.
#' @param to_pinyin A logic string to indicate whether the output is in pinyin spelling instead of Chinese characters. The default is FALSE.
#' @param to_dialect A character indicating the language transformation. At the prefectural level, valid transformation include `dia_group`,`dia_sub_group`. At the province level, valid transformation is `dia_super`. The default value is "none".
#' When `province` is TRUE, one can also choose `dia_super` to get the language zone of provinces.
#' @param province A logic string to indicate the level of converting. The default is FALSE.
#'
#'
#' @details In many national and regional data in China studies, the source applies incomplete names instead of the official, full name of a given region. A typical case is that "Xinjiang" is used much more often than "Xinjiang Weiwuer Zizhiqu" (the Xinjiang Uygur Autonomous Region) for the name of the province. In other cases the "Shi" (City) is often omitted to refer to a prefectural city. `regioncode` accounts this issue by offering the argument `incomplete_name`.
#' \itemize{
#' \item "none": no short name will be used for either input or output;
#' \item "from": input data is short names instead of the full, official ones;
#' \item "to": output results will be short names;
#' \item "both": both input and output are using short names.
#' }
#'
#' The argument makes a difference only when `code` or `name` are chose in `convert_to`.
#' Users can use this argument together with `name` to convert between names and incomplete names.
#'
#' @returns The function returns a character or numeric vector depending on what method is specified.
#'
#' @import pinyin
#'
#' @examples
#'
#'\dontrun{
#'# The example can be run well but CRAN does not like Chinese characters, so here just "dontrun" it.
#'
#' library(regioncode)
#'
#' regioncode(data_input = corruption$prefecture_id,
#' year_from = 2016,
#' year_to = 2017)
#'}
#'
#'
#' @export
#'
regioncode <- function(data_input,
year_from = 1999,
year_to = 2015,
convert_to = "code",
incomplete_name = FALSE,
zhixiashi = FALSE,
to_dialect = "none",
to_pinyin = FALSE,
province = FALSE) {
validate_input(data_input,
year_from,
province,
to_dialect,
convert_to,
zhixiashi,
to_pinyin)
if (province) {
zhixiashi <- FALSE
# 1 Section of province-level converting
if (to_dialect != "none") {
# 1-1 If convert language zone
year_from <- if (is.numeric(data_input[1]))
"prov_code"
else
"prov_name"
ls_index <- if (to_dialect == "dia_super") {
year_to <- "prov_language"
c(year_from, year_to)
}
} else {
# 1-2 If not convert language zone
prov_data <- unique(region_data[, c("prov_code", paste0("199", 8:9, "_nickname"), "area")])
# Because province nicknames changed in 1999
year_num <- ifelse(as.numeric(year_from) < 1999, 1998, 1999)
year_from <- if (is.numeric(data_input[1]))
"prov_code"
else
"prov_name"
year_to <- switch(
convert_to,
"name" = "prov_name",
"code" = "prov_code",
"area" = "area",
"nameToabbre" = {
year_from <- "prov_name"
paste0(year_num, "_nickname")
},
"codeToabbre" = {
year_from <- "prov_code"
paste0(year_num, "_nickname")
},
"abbreToname" = {
year_from <- paste0(year_num, "_nickname")
"prov_name"
},
"abbreTocode" = {
year_from <- paste0(year_num, "_nickname")
"prov_code"
},
"abbreToarea" = {
year_from <- paste0(year_num, "_nickname")
"area"
}
)
ls_index <- c(year_from, year_to)
}
} else {
# 2 Section of prefectural-level converting
if (to_dialect != "none") {
# 2-1 If convert language zone
year_from <- if (is.numeric(data_input[1]))
paste0(year_from, "_code")
else
paste0(year_from, "_name")
year_to <- if (to_dialect == "dia_group")
"pref_language_all"
else if (to_dialect == "dia_sub_group")
"dia_sub_language_all"
ls_index <- c(year_from, year_to)
} else {
# 2-2 If not convert language zone
year_from <- if (is.numeric(data_input[1]))
paste0(year_from, "_code")
else
paste0(year_from, "_name")
region_data <- region_data[!duplicated(region_data$`2019_code`), ]
year_to <- switch(
convert_to,
"code" = paste0(year_to, "_code"),
"area" = "area",
"name" = paste0(year_to, "_name"),
"rank" = paste0(year_to, "_rank")
)
ls_index <- c(year_from, year_to)
# Using the Municipal codes for within region codes
if (zhixiashi) {
region_zhixiashi <- subset(region_data, zhixiashi)
# Gathering all the needed fields
region_sname <- region_zhixiashi[grep("_sname$", names(region_zhixiashi))]
region_name <- region_zhixiashi[grep("_name$", names(region_zhixiashi))]
region_code <- region_zhixiashi[grep("_code$", names(region_zhixiashi))]
region_remain <- region_zhixiashi[!grepl(
"(_code$|_sname$|_name$|language$|_all$|_nickname$|dia_sub_group$|freq$)",names(region_zhixiashi))]
# Replacing prefectural names and codes with provincial ones
region_name2 <- as.data.frame(matrix(
rep(region_zhixiashi$prov_name, ncol(region_name)),
ncol = ncol(region_name)
))
names(region_name2) <- names(region_name)
region_sname2 <- as.data.frame(matrix(
rep(region_zhixiashi$prov_sname, ncol(region_sname)),
ncol = ncol(region_sname)
))
names(region_sname2) <- names(region_sname)
region_code2 <- as.data.frame(matrix(
rep(region_zhixiashi$prov_code, ncol(region_code)),
ncol = ncol(region_code)
))
names(region_code2) <- names(region_code)
region_zhixiashi <- cbind(region_name2,
region_code2,
region_sname2,
region_remain)
region_zhixiashi <- unique(region_zhixiashi[, order(names(region_zhixiashi))])
region_province <- unique(region_data[!grepl("language$|_all$|_nickname$|dia_sub_group$|freq$", names(region_data))])
region_province <- region_province[, order(names(region_province))]
region_data <- rbind(region_zhixiashi, region_province)
}
}
}
# Convert the input to a data.frame for later merging
data_input <- as.data.frame(data_input)
names(data_input) <- ls_index[1]
data_output <- unique(region_data[ls_index])
if (incomplete_name) {
data_input[[ls_index[1]]] <- substr(data_input[[ls_index[1]]], 1, 2)
data_output[[ls_index[1]]] <- substr(data_output[[ls_index[1]]], 1, 2)
}
# Indexing to find the first match quickly and directly
index <- match(data_input[[ls_index[1]]], data_output[[ls_index[1]]])
data_output <- data_output[index, year_to, drop = TRUE]
# Because '2pinyin' can not be used as a variable name
if (to_pinyin) {
# Predefined mapping for special cases with Chinese characters
special_cases <- c(
"\u9655\u897f" = "shaan_xi",
"\u5185\u8499" = "inner_mongolia",
"\u897f\u85cf" = "tibet",
"\u6fb3\u95e8" = "macao",
"\u9999\u6e2f" = "hong_kong"
)
# Extract the first two characters of each entry in data_output
first_two_chars <- substr(data_output, 1, 2)
# Apply special cases mapping
special_pinyin <- special_cases[first_two_chars]
# Use py function where no special case is matched
data_output <- ifelse(is.na(special_pinyin),
py(
char = first_two_chars,
dic = pydic(method = "toneless", dic = "pinyin2")
),
special_pinyin)
}
return(data_output)
}
# Function to validate input
validate_input <- function(data_input,
year_from,
province,
to_dialect,
convert_to,
zhixiashi,
to_pinyin) {
# Simplified data type checks
if (!(is.character(data_input[1]) || is.numeric(data_input[1]))) {
stop("Invalid input: only region names or division codes are valid.")
}
# Simplify type and value checks
if (!is.numeric(year_from) ||
!all(c(province, zhixiashi, to_pinyin) %in% c(TRUE, FALSE))) {
stop("Invalid input: Parameters must be of the correct data type.")
}
# Consolidated conversion checks
valid_conversions <- if (province && to_dialect == "none") {
c(
"name",
"code",
"area",
"rank",
"nameToabbre",
"codeToabbre",
"abbreToname",
"abbreTocode",
"abbreToarea"
)
} else {
c("name", "code", "area", "rank")
}
if (!(convert_to %in% valid_conversions)) {
stop("Invalid input: please choose a valid converting method.")
}
# Specific scenario checks
if (to_pinyin && convert_to == "code" && to_dialect == "none") {
stop("Invalid input: cannot translate administrative codes to pinyin.")
}
}