继续上次的内容,今天我们再来学习如何基于真实专利数据分城市、产业计算数实融合水平。
分城市
由于需要分城市,所以在保留变量的时候需要保留城市变量:
library(tidyverse) library(haven)
read_csv("patent_small/2012.csv") %>% filter(str_detect(IPC, ";")) %>% filter(!is.na(市)) %>% select(newipzlid, IPC, 市) %>% mutate( IPC = str_replace_all(IPC, " ", ""), ipc_list = str_split(IPC, ";"), ipc = map_chr(ipc_list, ~ paste(head(.x, 10), collapse = ";")) ) %>% select(newipzlid, ipc, city = 市) -> patent_data
patent_data
|
#> # A tibble: 466,842 × 3 #> newipzlid ipc city #> <dbl> <chr> <chr> #> 1 20120555529 G01C21/02;G01S19/00 上海市 #> 2 20120555530 G05B19/04;F42B15/01 上海市 #> 3 20121507067 G02B5/30;G03F7/20 上海市 #> 4 20120132658 B65B21/24;B65B53/02 上海市 #> 5 20121006311 H01J31/04;H01J29/52 上海市 #> 6 20120062954 F21L4/00;F21V21/06 上海市 #> 7 20121678429 H04L29/06;H04L12/40 上海市 #> 8 20121682445 G01S7/481;G01S17/89 上海市 #> 9 20120841479 H01H9/02;H01H45/02;H01H85/20 上海市 #> 10 20120079275 H02M1/00;H05K7/20 上海市 #> # ℹ 466,832 more rows
|
write_dta(patent_data, "ipcdata.dta")
|
然后就可以分城市计算了:
ipc_city <- patent_data %>% count(city, ipc, name = "freq")
ipc_city %>% group_by(city) %>% mutate(id = row_number()) %>% ungroup() %>% mutate(ipc_list = str_split(ipc, ";")) %>% unnest(ipc_list) %>% select(-ipc) %>% group_by(city, id) %>% summarize( ipc1 = list(ipc_list), ipc2 = list(ipc_list), freq = first(freq) ) %>% mutate( combinations = map2(ipc1, ipc2, ~ crossing(ipc1 = .x, ipc2 = .y)) ) %>% select(-ipc1, -ipc2, -id) %>% unnest(combinations) %>% ungroup() %>% filter(ipc1 != ipc2) %>% group_by(city, ipc1, ipc2) %>% summarize(value = sum(freq), .groups = "drop") -> ipc_matrix_city
ipc_matrix_city
|
#> # A tibble: 2,389,994 × 4 #> city ipc1 ipc2 value #> <chr> <chr> <chr> <int> #> 1 七台河市 A01C5/06 A01C7/06 1 #> 2 七台河市 A01C5/06 A01C7/20 1 #> 3 七台河市 A01C5/06 A01G25/09 1 #> 4 七台河市 A01C7/06 A01C5/06 1 #> 5 七台河市 A01C7/06 A01C7/20 1 #> 6 七台河市 A01C7/20 A01C5/06 1 #> 7 七台河市 A01C7/20 A01C7/06 1 #> 8 七台河市 A01D41/12 A01F12/22 1 #> 9 七台河市 A01F11/06 A01F12/18 1 #> 10 七台河市 A01F11/06 A01F12/44 1 #> # ℹ 2,389,984 more rows
|
write_dta(ipc_matrix_city, "ipcmat.dta")
|
后面的代码和前面的基本一样,就是分组的时候记得加上 city 的:
industry_reference <- read_dta("专利数据与行业小类代码简易对照表.dta") industry_classification <- read_dta("产业数实分类.dta")
ipc_matrix_city %>% rename(uniq_ipc = ipc1) %>% left_join(industry_reference, by = "uniq_ipc") %>% rename(industry1 = 国民经济行业代码) %>% select(-uniq_ipc) %>% rename(uniq_ipc = ipc2) %>% left_join(industry_reference, by = "uniq_ipc") %>% rename(industry2 = 国民经济行业代码) %>% select(-uniq_ipc) %>% filter(!is.na(industry1) & !is.na(industry2)) %>% mutate(value = ifelse(industry1 == industry2, 0, value)) %>% group_by(city, industry1, industry2) %>% summarize(value = sum(value), .groups = "drop") %>% filter(!is.na(industry1) & !is.na(industry2)) -> industry_matrix_city
industry_matrix_city
|
#> # A tibble: 5,294,894 × 4 #> city industry1 industry2 value #> <chr> <chr> <chr> <dbl> #> 1 七台河市 A0111 C3572 1 #> 2 七台河市 A0112 C3572 1 #> 3 七台河市 A0113 C3572 1 #> 4 七台河市 A0119 C3572 1 #> 5 七台河市 A0121 C3572 1 #> 6 七台河市 A0122 C3572 1 #> 7 七台河市 A0123 C3572 1 #> 8 七台河市 A0131 C3572 1 #> 9 七台河市 A0132 C3572 1 #> 10 七台河市 A0133 C3572 1 #> # ℹ 5,294,884 more rows
|
write_dta(industry_matrix_city, "industrymat.dta")
|
计算分城市数实融合:
# 此处代码需下载讲义材料查看~
fusion_city
|
#> # A tibble: 349 × 4 #> city 实实融合 数实融合 数数融合 #> <chr> <dbl> <dbl> <dbl> #> 1 深圳市 10.5 37.4 594. #> 2 北京市 16.5 30.0 554. #> 3 上海市 17.7 17.5 257. #> 4 台湾省 4.94 15.2 210. #> 5 苏州市 19.7 12.2 107. #> 6 杭州市 8.04 8.49 119. #> 7 日喀则市 5.06 8.25 NA #> 8 东莞市 4.98 5.48 58.5 #> 9 成都市 4.24 5.27 69.8 #> 10 玉树藏族自治州 11.5 5.07 NA #> # ℹ 339 more rows
|
# 显示分城市结果 print(fusion_city, n = 10)
|
#> # A tibble: 349 × 4 #> city 实实融合 数实融合 数数融合 #> <chr> <dbl> <dbl> <dbl> #> 1 深圳市 10.5 37.4 594. #> 2 北京市 16.5 30.0 554. #> 3 上海市 17.7 17.5 257. #> 4 台湾省 4.94 15.2 210. #> 5 苏州市 19.7 12.2 107. #> 6 杭州市 8.04 8.49 119. #> 7 日喀则市 5.06 8.25 NA #> 8 东莞市 4.98 5.48 58.5 #> 9 成都市 4.24 5.27 69.8 #> 10 玉树藏族自治州 11.5 5.07 NA #> # ℹ 339 more rows
|
分行业计算数实融合
分行业就不需要重新构造 IPC 融合矩阵了,而是直接在分行业计算那里替换上更细致的行业分类:
read_dta("国民经济分类与IPC分类号对照表.dta") %>% select(国民经济行业代码) %>% distinct() %>% mutate(industry2 = str_sub(国民经济行业代码, 2, -1)) %>% left_join(read_dta("数字经济核心产业.dta"), by = "industry2") %>% left_join(read_dta("实体产业分类.dta"), by = "国民经济行业代码") %>% select(国民经济行业代码, 数字经济产业, 实体产业) %>% mutate(class = as.numeric(!is.na(数字经济产业))) %>% mutate( 类别 = ifelse(!is.na(数字经济产业), "数字经济产业", 实体产业) ) %>% select(国民经济行业代码, 类别) %>% distinct() -> industry_classification2
industry_classification2
|
#> # A tibble: 821 × 2 #> 国民经济行业代码 类别 #> <chr> <chr> #> 1 A0111 农业 #> 2 A0112 农业 #> 3 A0113 农业 #> 4 A0119 农业 #> 5 A0121 农业 #> 6 A0122 农业 #> 7 A0123 农业 #> 8 A0131 农业 #> 9 A0132 农业 #> 10 A0133 农业 #> # ℹ 811 more rows
|
write_dta(industry_classification2, "产业数实分类2.dta")
|
然后计算:
这样就实现了分产业的计算。
点击这里跳转到 RStata 短书平台获取附件:名师讲堂|使用 R 语言测算数实融合水平(三):分城市、产业计算数实融合
评论