|
1 | | -# Name to codes of the same year |
2 | | -regioncode(data_input = corruption$prefecture, |
3 | | -convert_to = "code", |
4 | | -year_from = 2019, |
5 | | -year_to = 2019) |
6 | | -# Name to name of a different year |
7 | | -regioncode(data_input = corruption$prefecture, |
8 | | -convert_to = "name", |
9 | | -year_from = 2019, |
10 | | -year_to = 1989) |
11 | | -# Original full names |
12 | | -corruption$prefecture |
13 | | -fake_incomplete <- corruption$prefecture |
14 | | -index_incomplete <- sample(seq(length(corruption$prefecture)), 7) |
15 | | -fake_incomplete[index_incomplete] <- fake_incomplete[index_incomplete] |> |
16 | | -substr(start = 1, stop = 2) |
17 | | -fake_incomplete |
18 | | -# Conversion to full names in 2008 |
19 | | -regioncode(data_input = fake_incomplete, |
20 | | -convert_to = "name", |
21 | | -year_from = 2019, |
22 | | -year_to = 2008, |
23 | | -incomplete_name = TRUE) |
24 | | -names_municipality <- c("北京市", # Beijing, a municipality |
25 | | -"海淀区", # A district of Beijing |
26 | | -"上海市", # Shanghai, a municipality |
27 | | -"静安区", # A district of Shanghai |
28 | | -"济南市") # A prefecture of Shandong |
29 | | -# When `zhixiashi` is FALSE, only the districts are recognized |
30 | | -regioncode(data_input = names_municipality, |
31 | | -year_from = 2019, |
32 | | -year_to = 2019, |
33 | | -convert_to = "code", |
34 | | -zhixiashi = FALSE) |
35 | | -# When `zhixiashi` is TRUE, municipalities are recognized |
36 | | -regioncode(data_input = names_municipality, |
37 | | -year_from = 2019, |
38 | | -year_to = 2019, |
39 | | -convert_to = "code", |
40 | | -zhixiashi = TRUE) |
41 | | -tibble( |
42 | | -city = corruption$prefecture, |
43 | | -rank1989 = regioncode(data_input = corruption$prefecture, |
44 | | -year_from = 2019, |
45 | | -year_to = 1989, |
46 | | -convert_to="rank"), |
47 | | -rank2014 = regioncode(data_input = corruption$prefecture, |
48 | | -year_from = 2019, |
49 | | -year_to = 2014, |
50 | | -convert_to = "rank") |
51 | | -) |
52 | | -tibble( |
53 | | -city = corruption$prefecture, |
54 | | -cityPY = regioncode(data_input = corruption$prefecture, |
55 | | -year_from = 2019, |
56 | | -year_to = 1989, |
57 | | -convert_to = "name", |
58 | | -to_pinyin = TRUE |
59 | | -), |
60 | | -areaPY = regioncode(data_input = corruption$prefecture, |
61 | | -year_from = 2019, |
62 | | -year_to = 1989, |
63 | | -convert_to = "area", |
64 | | -to_pinyin = TRUE |
65 | | -) |
66 | | -) |
67 | | -# Regions with special spelling |
68 | | -regioncode(data_input = c("山西", "陕西", "内蒙古", "香港", "澳门"), |
69 | | -year_from = 2019, |
70 | | -year_to = 2008, |
71 | | -convert_to = "name", |
72 | | -incomplete_name = TRUE, |
73 | | -province = TRUE, |
74 | | -to_pinyin = TRUE |
75 | | -) |
76 | | -tibble( |
77 | | -province = corruption$province_id, |
78 | | -prov_name = regioncode(data_input = corruption$province_id, |
79 | | -convert_to = "name", |
80 | | -year_from = 2019, |
81 | | -year_to = 1989, |
82 | | -province = TRUE), |
83 | | -prov_abbre = regioncode(data_input = corruption$province_id, |
84 | | -convert_to = "codeToabbre", |
85 | | -year_from = 2019, |
86 | | -year_to = 1989, |
87 | | -province = TRUE) |
88 | | -) |
89 | | -regioncode(data_input = corruption$prefecture, |
90 | | -year_from = 2019, |
91 | | -year_to = 1989, |
92 | | -convert_to = "area") |
93 | | -tibble( |
94 | | -city = corruption$prefecture, |
95 | | -dialectGroup = regioncode(data_input = corruption$prefecture, |
96 | | -year_from = 2019, |
97 | | -year_to = 1989, |
98 | | -to_dialect = "dia_group"), |
99 | | -dialectSubGroup = regioncode(data_input = corruption$prefecture, |
100 | | -year_from = 2019, |
101 | | -year_to = 1989, |
102 | | -to_dialect = "dia_sub_group") |
103 | | -) |
104 | | -pkgbuild::check_build_tools(debug = TRUE) |
105 | | -library(devtools) |
106 | | -detach("package:devtools", unload = TRUE) |
107 | | -install.packages("devtools") |
108 | | -pkgbuild::check_build_tools(debug = TRUE) |
109 | | -new<- import("~/Desktop/全国各市城镇人口构成.xlsx") |
110 | | -if (!requireNamespace("dplyr", quietly = TRUE)) { |
111 | | -install.packages("dplyr") |
112 | | -} |
113 | | -library(dplyr) |
114 | | -if (!requireNamespace("pacman", quietly = TRUE)) { |
115 | | -install.packages("pacman") |
116 | | -} |
117 | | -library(pacman) |
118 | | -p_load("rio", |
119 | | -"tidyverse") |
120 | | -new<- import("~/Desktop/全国各市城镇人口构成.xlsx") |
121 | | -View(new) |
122 | | -str(new) |
123 | | -new <- new %>% |
124 | | -mutate(population= as.numeric(population)) |
125 | | -View(new) |
126 | | -new<- import("~/Desktop/全国各市城镇人口构成.xlsx") |
127 | | -new <- new %>% |
128 | | -mutate(population= as.numeric(population)) |
129 | | -View(new) |
130 | | -new<- import("~/Desktop/全国各市城镇人口构成.xlsx") |
131 | | -View(new) |
132 | | -new <- new %>% |
133 | | -mutate(population= as.numeric(population)) |
134 | | -new<- import("~/Desktop/全国各市城镇人口构成.xlsx") |
135 | | -new <- new %>% |
136 | | -mutate(population= as.numeric(population)) |
137 | | -View(new) |
138 | | -new <- new %>% |
139 | | -mutate(population = as.numeric(population)) %>% |
140 | | -arrange(code, year) |
141 | | -View(new) |
142 | | -generate_cityranking <- function(df) { |
143 | | -year <- unique(df$year) |
144 | | -year_cityranking <- paste0(year, "_cityranking") |
145 | | -if (year %in% 1986:2013) { |
146 | | -df[[year_cityranking]] <- case_when( |
147 | | -df$population > 100 ~ "特大城市", |
148 | | -df$population > 50 & df$population <= 100 ~ "大城市", |
149 | | -df$population > 20 & df$population <= 50 ~ "中等城市", |
150 | | -df$population <= 20 ~ "小城市", |
151 | | -TRUE ~ NA_character_ |
152 | | -) |
153 | | -} else if (year %in% 2014:2019) { |
154 | | -df[[year_cityranking]] <- case_when( |
155 | | -df$population > 1000 ~ "超大城市", |
156 | | -df$population > 500 & df$population <= 1000 ~ "特大城市", |
157 | | -df$population > 300 & df$population <= 500 ~ "I型大城市", |
158 | | -df$population > 100 & df$population <= 300 ~ "II型大城市", |
159 | | -df$population > 50 & df$population <= 100 ~ "中等城市", |
160 | | -df$population > 20 & df$population <= 50 ~ "I型小城市", |
161 | | -df$population <= 20 ~ "II型小城市", |
162 | | -TRUE ~ NA_character_ |
163 | | -) |
164 | | -} |
165 | | -df <- select(df, -year, -population,-population_original) |
166 | | -df |
167 | | -} |
168 | | -new_cityranking <- function(df, year) { |
169 | | -year_cityranking <- paste0(year, "_cityranking") |
170 | | -if (year %in% 1986:2013) { |
171 | | -df[[year_cityranking]] <- case_when( |
172 | | -df$population > 100 ~ "特大城市", |
173 | | -df$population > 50 & df$population <= 100 ~ "大城市", |
174 | | -df$population > 20 & df$population <= 50 ~ "中等城市", |
175 | | -df$population <= 20 ~ "小城市", |
176 | | -TRUE ~ NA_character_ |
177 | | -) |
178 | | -} else if (year %in% 2014:2019) { |
179 | | -df[[year_cityranking]] <- case_when( |
180 | | -df$population > 1000 ~ "超大城市", |
181 | | -df$population > 500 & df$population <= 1000 ~ "特大城市", |
182 | | -df$population > 300 & df$population <= 500 ~ "I型大城市", |
183 | | -df$population > 100 & df$population <= 300 ~ "II型大城市", |
184 | | -df$population > 50 & df$population <= 100 ~ "中等城市", |
185 | | -df$population > 20 & df$population <= 50 ~ "I型小城市", |
186 | | -df$population <= 20 ~ "II型小城市", |
187 | | -TRUE ~ NA_character_ |
188 | | -) |
189 | 1 | } |
190 | 2 | df <- df %>% select(-year, -population) |
191 | 3 | return(df) |
@@ -510,3 +322,191 @@ region_data <- read_excel("region_data.xlsx") |
510 | 322 | View(region_data) |
511 | 323 | View(corruption) |
512 | 324 | save(region_data, corruption, file = "~/Documents/GitHub/regioncode/R/sysdata.rda") |
| 325 | +devtools::check() |
| 326 | +## ----setup, include=FALSE----------------------------------------------------- |
| 327 | +knitr::opts_chunk$set(message = FALSE, warning = FALSE) |
| 328 | +if(!require(regioncode)) install.packages("regioncode") |
| 329 | +library(regioncode) |
| 330 | +library(dplyr) |
| 331 | +## ----code2code---------------------------------------------------------------- |
| 332 | +library(regioncode) |
| 333 | +data("corruption") |
| 334 | +# Conversion to the 1989 version |
| 335 | +regioncode(data_input = corruption$prefecture_id, |
| 336 | +convert_to = "code", # default setting |
| 337 | +year_from = 2019, |
| 338 | +year_to = 1989) |
| 339 | +# Comparison |
| 340 | +tibble( |
| 341 | +code2019 = corruption$prefecture_id, |
| 342 | +code1989 = regioncode(data_input = corruption$prefecture_id, |
| 343 | +convert_to = "code", # default setting |
| 344 | +year_from = 2019, |
| 345 | +year_to = 1989), |
| 346 | +name2019 = regioncode(data_input = corruption$prefecture_id, |
| 347 | +convert_to = "name", # default setting |
| 348 | +year_from = 2019, |
| 349 | +year_to = 2019), |
| 350 | +name1989 = regioncode(data_input = corruption$prefecture_id, |
| 351 | +convert_to = "name", # default setting |
| 352 | +year_from = 2019, |
| 353 | +year_to = 1989) |
| 354 | +) |
| 355 | +## ----setup, include=FALSE----------------------------------------------------- |
| 356 | +knitr::opts_chunk$set(message = FALSE, warning = FALSE) |
| 357 | +if(!require(regioncode)) install.packages("regioncode") |
| 358 | +library(regioncode) |
| 359 | +library(dplyr) |
| 360 | +## ----code2code---------------------------------------------------------------- |
| 361 | +library(regioncode) |
| 362 | +data("corruption") |
| 363 | +# Conversion to the 1989 version |
| 364 | +regioncode(data_input = corruption$prefecture_id, |
| 365 | +convert_to = "code", # default setting |
| 366 | +year_from = 2019, |
| 367 | +year_to = 1989) |
| 368 | +# Comparison |
| 369 | +tibble( |
| 370 | +code2019 = corruption$prefecture_id, |
| 371 | +code1989 = regioncode(data_input = corruption$prefecture_id, |
| 372 | +convert_to = "code", # default setting |
| 373 | +year_from = 2019, |
| 374 | +year_to = 1989), |
| 375 | +name2019 = regioncode(data_input = corruption$prefecture_id, |
| 376 | +convert_to = "name", # default setting |
| 377 | +year_from = 2019, |
| 378 | +year_to = 2019), |
| 379 | +name1989 = regioncode(data_input = corruption$prefecture_id, |
| 380 | +convert_to = "name", # default setting |
| 381 | +year_from = 2019, |
| 382 | +year_to = 1989) |
| 383 | +) |
| 384 | +## ----code2name---------------------------------------------------------------- |
| 385 | +# Original name |
| 386 | +tibble( |
| 387 | +id = corruption$prefecture_id, |
| 388 | +name = corruption$prefecture |
| 389 | +) |
| 390 | +# Codes to name |
| 391 | +regioncode(data_input = corruption$prefecture_id, |
| 392 | +convert_to = "name", |
| 393 | +year_from = 2019, |
| 394 | +year_to = 1989) |
| 395 | +# Name to codes of the same year |
| 396 | +regioncode(data_input = corruption$prefecture, |
| 397 | +convert_to = "code", |
| 398 | +year_from = 2019, |
| 399 | +year_to = 2019) |
| 400 | +# Name to name of a different year |
| 401 | +regioncode(data_input = corruption$prefecture, |
| 402 | +convert_to = "name", |
| 403 | +year_from = 2019, |
| 404 | +year_to = 1989) |
| 405 | +## ----incomplete_name---------------------------------------------------------- |
| 406 | +# Original full names |
| 407 | +corruption$prefecture |
| 408 | +fake_incomplete <- corruption$prefecture |
| 409 | +index_incomplete <- sample(seq(length(corruption$prefecture)), 7) |
| 410 | +fake_incomplete[index_incomplete] <- fake_incomplete[index_incomplete] |> |
| 411 | +substr(start = 1, stop = 2) |
| 412 | +fake_incomplete |
| 413 | +# Conversion to full names in 2008 |
| 414 | +regioncode(data_input = fake_incomplete, |
| 415 | +convert_to = "name", |
| 416 | +year_from = 2019, |
| 417 | +year_to = 2008, |
| 418 | +incomplete_name = TRUE) |
| 419 | +## ----municipality------------------------------------------------------------- |
| 420 | +names_municipality <- c("北京市", # Beijing, a municipality |
| 421 | +"海淀区", # A district of Beijing |
| 422 | +"上海市", # Shanghai, a municipality |
| 423 | +"静安区", # A district of Shanghai |
| 424 | +"济南市") # A prefecture of Shandong |
| 425 | +# When `zhixiashi` is FALSE, only the districts are recognized |
| 426 | +regioncode(data_input = names_municipality, |
| 427 | +year_from = 2019, |
| 428 | +year_to = 2019, |
| 429 | +convert_to = "code", |
| 430 | +zhixiashi = FALSE) |
| 431 | +# When `zhixiashi` is TRUE, municipalities are recognized |
| 432 | +regioncode(data_input = names_municipality, |
| 433 | +year_from = 2019, |
| 434 | +year_to = 2019, |
| 435 | +convert_to = "code", |
| 436 | +zhixiashi = TRUE) |
| 437 | +## ----rank--------------------------------------------------------------------- |
| 438 | +tibble( |
| 439 | +city = corruption$prefecture, |
| 440 | +rank1989 = regioncode(data_input = corruption$prefecture, |
| 441 | +year_from = 2019, |
| 442 | +year_to = 1989, |
| 443 | +convert_to="rank"), |
| 444 | +rank2014 = regioncode(data_input = corruption$prefecture, |
| 445 | +year_from = 2019, |
| 446 | +year_to = 2014, |
| 447 | +convert_to = "rank") |
| 448 | +) |
| 449 | +## ----pinyin------------------------------------------------------------------- |
| 450 | +tibble( |
| 451 | +city = corruption$prefecture, |
| 452 | +cityPY = regioncode(data_input = corruption$prefecture, |
| 453 | +year_from = 2019, |
| 454 | +year_to = 1989, |
| 455 | +convert_to = "name", |
| 456 | +to_pinyin = TRUE |
| 457 | +), |
| 458 | +areaPY = regioncode(data_input = corruption$prefecture, |
| 459 | +year_from = 2019, |
| 460 | +year_to = 1989, |
| 461 | +convert_to = "area", |
| 462 | +to_pinyin = TRUE |
| 463 | +) |
| 464 | +) |
| 465 | +# Regions with special spelling |
| 466 | +regioncode(data_input = c("山西", "陕西", "内蒙古", "香港", "澳门"), |
| 467 | +year_from = 2019, |
| 468 | +year_to = 2008, |
| 469 | +convert_to = "name", |
| 470 | +incomplete_name = TRUE, |
| 471 | +province = TRUE, |
| 472 | +to_pinyin = TRUE |
| 473 | +) |
| 474 | +## ----provinces---------------------------------------------------------------- |
| 475 | +tibble( |
| 476 | +province = corruption$province_id, |
| 477 | +prov_name = regioncode(data_input = corruption$province_id, |
| 478 | +convert_to = "name", |
| 479 | +year_from = 2019, |
| 480 | +year_to = 1989, |
| 481 | +province = TRUE), |
| 482 | +prov_abbre = regioncode(data_input = corruption$province_id, |
| 483 | +convert_to = "codeToabbre", |
| 484 | +year_from = 2019, |
| 485 | +year_to = 1989, |
| 486 | +province = TRUE) |
| 487 | +) |
| 488 | +## ----2area-------------------------------------------------------------------- |
| 489 | +regioncode(data_input = corruption$prefecture, |
| 490 | +year_from = 2019, |
| 491 | +year_to = 1989, |
| 492 | +convert_to = "area") |
| 493 | +## ----language_zone------------------------------------------------------------ |
| 494 | +tibble( |
| 495 | +city = corruption$prefecture, |
| 496 | +dialectGroup = regioncode(data_input = corruption$prefecture, |
| 497 | +year_from = 2019, |
| 498 | +year_to = 1989, |
| 499 | +to_dialect = "dia_group"), |
| 500 | +dialectSubGroup = regioncode(data_input = corruption$prefecture, |
| 501 | +year_from = 2019, |
| 502 | +year_to = 1989, |
| 503 | +to_dialect = "dia_sub_group") |
| 504 | +) |
| 505 | +load("~/Develop/regioncode/R/sysdata.rda") |
| 506 | +load("~/Develop/regioncode/data/corruption.rda") |
| 507 | +#| label: installConfirm |
| 508 | +R.Version() |
| 509 | +#| label: installConfirm |
| 510 | +R.Version() |
| 511 | +letters |
| 512 | +1:50 |
0 commit comments