更换前:11-2022年统计用区划代码和城乡划分代码-北京市.xlsx
更换后:11-2022Beijingshi.xlsx
#代码--------------------------
library(readxl)
library(dplyr)
library(openxlsx)
# 设置工作目录,这里假设所有的xlsx文件都在同一个目录下
setwd("E:/GIS data/全国数据/统计年鉴/【2022.12】全国五级行政区划代码、城乡划分代码(省-市-区县-乡镇-村)/shuju")
# 获取所有xlsx文件的文件名
file_names <- list.files(pattern = "\\.xlsx$", full.names = TRUE)
# 创建一个空的数据框
combined_data <- data.frame()
# 循环读取每个xlsx文件
for (file in file_names) {
# 读取文件中的所有sheet
data <- read.xlsx(file)
# 提取文件名作为标识符(2),并且进行替换
filename <- gsub("年统计用区划代码和城乡划分代码-", "", file)
filename=gsub("北京","Beijing",filename)
filename=gsub("天津","Tianjin",filename)
filename=gsub("河北","Hebei",filename)
filename=gsub("山西","Shanxi",filename)
filename=gsub("内蒙古","Inner Mongolia",filename)
filename=gsub("辽宁","Liaoning",filename)
filename=gsub("吉林","Jilin",filename)
filename=gsub("黑龙江","Heilongjiang",filename)
filename=gsub("上海","Shanghai",filename)
filename=gsub("江苏","Jiangsu",filename)
filename=gsub("浙江","Zhejiang",filename)
filename=gsub("安徽","Anhui",filename)
filename=gsub("福建","Fujian",filename)
filename=gsub("江西","Jiangxi",filename)
filename=gsub("山东","Shandong",filename)
filename=gsub("河南","Henan",filename)
filename=gsub("湖北","Hubei",filename)
filename=gsub("湖南","Hunan",filename)
filename=gsub("广东","Guangdong",filename)
filename=gsub("广西","Guangxi",filename)
filename=gsub("海南","Hainan",filename)
filename=gsub("重庆","Chongqing",filename)
filename=gsub("四川","Sichuan",filename)
filename=gsub("贵州","Guizhou",filename)
filename=gsub("云南","Yunnan",filename)
filename=gsub("西藏","Tibet",filename)
filename=gsub("陕西","Shaanxi",filename)
filename=gsub("甘肃","Gansu",filename)
filename=gsub("青海","Qinghai",filename)
filename=gsub("宁夏","Ningxia",filename)
filename=gsub("新疆","Xinjiang",filename)
filename=gsub("台湾","Taiwan",filename)
filename=gsub("香港特别行政区","Hong Kong",filename)
filename=gsub("澳门特别行政区","Macau",filename)
filename=gsub("省","sheng",filename)
filename=gsub("市","shi",filename)
output_dir <- "E:/GIS data/全国数据/统计年鉴/【2022.12】全国五级行政区划代码、城乡划分代码(省-市-区县-乡镇-村)/name"#指定输出路径
output_filename <- file.path(output_dir, paste0(filename)) # 名根据特定的字符生成输出文件名 & 拼接路径和文件名
merged_data1=data
write.xlsx(merged_data1, output_filename)}#输出文件,data为数据文件