본문 바로가기
  • Homines, dum docent, discunt
  • Repetitio est Mater Memoriae
  • Dilige et fac quod vis
지식 창고/R 언어

KOSIS 통계표 정리

by Manana Cho 2022. 6. 17.
반응형

KOSISLISTID.zip
4.10MB

###Install Packages/Libraries-------------------------------
if (!require('httr')) install.packages('httr'); library('httr')
if (!require('tidyverse')) install.packages('tidyverse'); library('tidyverse')
if (!require('jsonlite')) install.packages('jsonlite'); library('jsonlite')
if (!require('rvest')) install.packages('rvest'); library('rvest')
if (!require('dplyr')) install.packages('dplyr'); library('dplyr')
if (!require('stringr')) install.packages('stringr'); library('stringr')
if (!require('magrittr')) install.packages('magrittr'); library('magrittr')
if (!require('shiny')) install.packages('shiny'); library('shiny')
if (!require('DT')) install.packages('DT'); library('DT')
if (!require('XML')) install.packages('XML'); library('XML')

###API_KEY---------------------------------------------------
api_key= "api_key"
userStatsId="[개인아이디]" #KOSIS ID

###[1] 통계목록----------------------------------------------
base_url='https://kosis.kr/openapi/statisticsList.do'
v_vwCd ='MT_ZTITLE'
#BY EACH THEME
v_parentListId ='H1'
stat_list_url=paste0('http://kosis.kr/openapi/statisticsList.do',
'?method=','getList',
'&apiKey=',api_key,
'&vwCd=','MT_ZTITLE',
'&parentListId=',v_parentListId,
'&format=','json',
'&jsonVD=','Y')
stat_list=GET(url=stat_list_url)
stat_list
status_code(x = stat_list) #값이 있는지 확인하는 코드입니다.
stat_list_json=content(stat_list, as='text')
stat_list_df=fromJSON(txt = stat_list_json %>%
as.character()) %>%
tbl_df
stat_list_df
colnames(stat_list_df)=c("목록명","서비스뷰","서비스명","목록ID")
DT::datatable(stat_list_df) ###통계목록 일부

#BY ALL
#STR 루프가 작동하지 않아 수기로 작성했습니다. 양해를 부탁드립니다.
#LIST NAME TABLE-----------------------------------------------------------------
LIST.FM=data.frame(한글명=c("인구",
"사회일반",
"범죄ㆍ안전",
"노동",
"소득ㆍ소비ㆍ자산",
"보건",
"복지",
"교육ㆍ훈련",
"문화ㆍ여가",
"주거",
"국토이용",
"경제일반ㆍ경기",
"기업경영",
"농림",
"수산",
"광업ㆍ제조업",
"건설",
"교통ㆍ물류",
"정보통신",
"과학ㆍ기술",
"도소매ㆍ서비스",
"임금",
"물가",
"국민계정",
"정부ㆍ재정",
"금융",
"무역ㆍ국제수지",
"환경",
"에너지",
"지역통계"),
LIST_ID=c("A",
"B",
"C",
"D",
"E",
"F",
"G",
"H1",
"H2",
"I1",
"I2",
"J1",
"J2",
"K1",
"K2",
"L",
"M1",
"M2",
"N1",
"N2",
"O",
"P1",
"P2",
"Q",
"R",
"S1",
"S2",
"T",
"U",
"V"),
변수명=c("liststat1",
"liststat2",
"liststat3",
"liststat4",
"liststat5",
"liststat6",
"liststat7",
"liststat8",
"liststat9",
"liststat10",
"liststat11",
"liststat12",
"liststat13",
"liststat14",
"liststat15",
"liststat16",
"liststat17",
"liststat18",
"liststat19",
"liststat20",
"liststat21",
"liststat22",
"liststat23",
"liststat24",
"liststat25",
"liststat26",
"liststat27",
"liststat28",
"liststat29",
"liststat30"
)
)

#URL FOR ALL LIST TYPE-----------------------------------------------------------------
liststat1=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=A&format=json&jsonVD=Y"))
liststat2=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=B&format=json&jsonVD=Y"))
liststat3=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=C&format=json&jsonVD=Y"))
liststat4=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=D&format=json&jsonVD=Y"))
liststat5=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=E&format=json&jsonVD=Y"))
liststat6=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=F&format=json&jsonVD=Y"))
liststat7=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=G&format=json&jsonVD=Y"))
liststat8=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=H1&format=json&jsonVD=Y"))
liststat9=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=H2&format=json&jsonVD=Y"))
liststat10=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=I1&format=json&jsonVD=Y"))
liststat11=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=I2&format=json&jsonVD=Y"))
liststat12=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=J1&format=json&jsonVD=Y"))
liststat13=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=J2&format=json&jsonVD=Y"))
liststat14=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=K1&format=json&jsonVD=Y"))
liststat15=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=K2&format=json&jsonVD=Y"))
liststat16=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=L&format=json&jsonVD=Y"))
liststat17=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=M1&format=json&jsonVD=Y"))
liststat18=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=M2&format=json&jsonVD=Y"))
liststat19=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=N1&format=json&jsonVD=Y"))
liststat20=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=N2&format=json&jsonVD=Y"))
liststat21=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=O&format=json&jsonVD=Y"))
liststat22=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=P1&format=json&jsonVD=Y"))
liststat23=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=P2&format=json&jsonVD=Y"))
liststat24=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=Q&format=json&jsonVD=Y"))
liststat25=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=R&format=json&jsonVD=Y"))
liststat26=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=S1&format=json&jsonVD=Y"))
liststat27=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=S2&format=json&jsonVD=Y"))
liststat28=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=T&format=json&jsonVD=Y"))
liststat29=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=U&format=json&jsonVD=Y"))
liststat30=GET(paste0("http://kosis.kr/openapi/statisticsList.do?method=getList&apiKey=",api_key,"&vwCd=MT_ZTITLE&parentListId=V&format=json&jsonVD=Y"))
#-----------------------------------------------------
#READ JSON TYPE LIST--------------------------------------------------------
stat_list_json_1=content(liststat1, as='text')
stat_list_json_1 =content(liststat1, as='text')
stat_list_1=fromJSON(txt = stat_list_json_1 %>%
as.character()) %>%
tbl_df
stat_list_json_2 =content(liststat2, as='text')
stat_list_2=fromJSON(txt = stat_list_json_2 %>%
as.character()) %>%
tbl_df
stat_list_json_3 =content(liststat3, as='text')
stat_list_3=fromJSON(txt = stat_list_json_3 %>%
as.character()) %>%
tbl_df
stat_list_json_4 =content(liststat4, as='text')
stat_list_4=fromJSON(txt = stat_list_json_4 %>%
as.character()) %>%
tbl_df
stat_list_json_5 =content(liststat5, as='text')
stat_list_5=fromJSON(txt = stat_list_json_5 %>%
as.character()) %>%
tbl_df
stat_list_json_6 =content(liststat6, as='text')
stat_list_6=fromJSON(txt = stat_list_json_6 %>%
as.character()) %>%
tbl_df
stat_list_json_7 =content(liststat7, as='text')
stat_list_7=fromJSON(txt = stat_list_json_7 %>%
as.character()) %>%
tbl_df
stat_list_json_8 =content(liststat8, as='text')
stat_list_8=fromJSON(txt = stat_list_json_8 %>%
as.character()) %>%
tbl_df
stat_list_json_9 =content(liststat9, as='text')
stat_list_9=fromJSON(txt = stat_list_json_9 %>%
as.character()) %>%
tbl_df
stat_list_json_10 =content(liststat10, as='text')
stat_list_10=fromJSON(txt = stat_list_json_10 %>%
as.character()) %>%
tbl_df
stat_list_json_11 =content(liststat11, as='text')
stat_list_11=fromJSON(txt = stat_list_json_11 %>%
as.character()) %>%
tbl_df
stat_list_json_12 =content(liststat12, as='text')
stat_list_12=fromJSON(txt = stat_list_json_12 %>%
as.character()) %>%
tbl_df
stat_list_json_13 =content(liststat13, as='text')
stat_list_13=fromJSON(txt = stat_list_json_13 %>%
as.character()) %>%
tbl_df
stat_list_json_14 =content(liststat14, as='text')
stat_list_14=fromJSON(txt = stat_list_json_14 %>%
as.character()) %>%
tbl_df
stat_list_json_15 =content(liststat15, as='text')

stat_list_15=fromJSON(txt = stat_list_json_15 %>%
as.character()) %>%
tbl_df
stat_list_json_16 =content(liststat16, as='text')
stat_list_16=fromJSON(txt = stat_list_json_16 %>%
as.character()) %>%
tbl_df
stat_list_json_17 =content(liststat17, as='text')
stat_list_17=fromJSON(txt = stat_list_json_17 %>%
as.character()) %>%
tbl_df
stat_list_json_18 =content(liststat18, as='text')
stat_list_18=fromJSON(txt = stat_list_json_18 %>%
as.character()) %>%
tbl_df
stat_list_json_19 =content(liststat19, as='text')

stat_list_19=fromJSON(txt = stat_list_json_19 %>%
as.character()) %>%
tbl_df
stat_list_json_20 =content(liststat20, as='text')
stat_list_20=fromJSON(txt = stat_list_json_20 %>%
as.character()) %>%
tbl_df
stat_list_json_21 =content(liststat21, as='text')
stat_list_21=fromJSON(txt = stat_list_json_21 %>%
as.character()) %>%
tbl_df
stat_list_json_22 =content(liststat22, as='text')
stat_list_22=fromJSON(txt = stat_list_json_22 %>%
as.character()) %>%
tbl_df
stat_list_json_23 =content(liststat23, as='text')
stat_list_23=fromJSON(txt = stat_list_json_23 %>%
as.character()) %>%
tbl_df
stat_list_json_24 =content(liststat24, as='text')
stat_list_24=fromJSON(txt = stat_list_json_24 %>%
as.character()) %>%
tbl_df
stat_list_json_25 =content(liststat25, as='text')
stat_list_25=fromJSON(txt = stat_list_json_25 %>%
as.character()) %>%
tbl_df
stat_list_json_26 =content(liststat26, as='text')
stat_list_26=fromJSON(txt = stat_list_json_26 %>%
as.character()) %>%
tbl_df
stat_list_json_27 =content(liststat27, as='text')
stat_list_27=fromJSON(txt = stat_list_json_27 %>%
as.character()) %>%
tbl_df
stat_list_json_28 =content(liststat28, as='text')
stat_list_28=fromJSON(txt = stat_list_json_28 %>%
as.character()) %>%
tbl_df
stat_list_json_29 =content(liststat29, as='text')
stat_list_29=fromJSON(txt = stat_list_json_29 %>%
as.character()) %>%
tbl_df
stat_list_json_30 =content(liststat30, as='text')
stat_list_30=fromJSON(txt = stat_list_json_30 %>%
as.character()) %>%
tbl_df
#CONCATENATE ALL LIST BY TYPE INTO ALL------------------------------------------------------
list_df_all=rbind(stat_list_1
, stat_list_2
, stat_list_3
, stat_list_4
, stat_list_5
, stat_list_6
, stat_list_7
, stat_list_8
, stat_list_9
, stat_list_10
, stat_list_11
, stat_list_12
, stat_list_13
, stat_list_14
, stat_list_15
, stat_list_16
, stat_list_17
, stat_list_18
, stat_list_19
, stat_list_20
, stat_list_21
, stat_list_22
, stat_list_23
, stat_list_24
, stat_list_25
, stat_list_26
, stat_list_27
, stat_list_28
, stat_list_29
, stat_list_30)
list_df_all$LIST_CAT=str_sub(list_df_all$LIST_ID,1,2)
list_df_all$LIST_ID=gsub("_","",list_df_all$LIST_CAT)
LIST_CAT2=merge(list_df_all,LIST.FM,by="LIST_ID")
list_df_all=LIST_CAT2[c(1:4,6)]

###---------------------------------------------------------------------
DT::datatable(list_df_all)
write.csv(list_df_all,"list_df_all.csv") ###통계목록 중간본
###[1-a] 통계ID 불러오기(수동_ OPEN API로 확인 불가)----------------
KOSISLISTID=read.csv('KOSISLISTID.csv')
str(KOSISLISTID)

#view(KOSISLISTID)
KOSISLISTIDdt=KOSISLISTID[c(1,2,8,9)]
KOSISLISTIDdt
shinyApp(
ui = fluidPage(DTOutput('tbl')),
server = function(input, output) {
output$tbl = renderDT(
KOSISLISTIDdt, options = list(lengthChange = FALSE)
)
}
) #조회가능한 통계표
###[2] 통계자료----------------------------------------------
###[2-a]전국사업체조사 (DT_1K50001:) #시계열은 인수 선택이 불가하여 횡단면 조사로 항목을 포함시키고, 이후 시점을 설정할 수 있게 하였습니다.
newEstPrdCnt=1 # 최근시점 조정에 사용할 수 있는 항목으로 현재 시점부터 사용할 기준시점까지의 간격을 입력
#DT_1K52C07(시도·산업·대표자연령대별 사업체수(’12~ )) ----------------------------------
{"
DT_1K52C07=GET(paste0("https://kosis.kr/openapi/statisticsData.do?method=getList&apiKey=",api_key,
"&format=json&jsonVD=Y&userStatsId=",userStatsId,
"/101/DT_1K52C07/2/1/20220503163345&prdSe=Y&newEstPrdCnt=1&prdSe=Y&newEstPrdCnt=",
newEstPrdCnt)) ##SETTING YR
"} #데이터 수 초과로 작동하지 않는 코드입니다. 예시코드로 남겨두었습니다. 아래의 개별 코드는 작동합니다.
url_temp1="https://kosis.kr/openapi/statisticsData.do?method=getList&apiKey=api_key=&format=json&jsonVD=Y&userStatsId=개인kosis아이디/101/DT_1K52C07/2/1/20220503164054&prdSe=Y&newEstPrdCnt=1"
DT_1K52C07=GET(url_temp1)
DT_1K52C07_json =content(DT_1K52C07, as='text')
DT_1K52C07=fromJSON(txt = DT_1K52C07_json %>%
as.character()) %>%
tbl_df
DT_1K52C07
DT::datatable(DT_1K52C07) ###통계목록 일부
write.csv(DT_1K52C07,"DT_1K52C07.csv")

반응형

댓글