Function

コメント・参照等
  • 年齢階層別失業率ファイル(総務省)を読み込み整形する関数

FUN = f_read_UnemploymentByAge.r
function (download_folder) 
{
    lapply(c("XLConnect", "Nippon"), require, character.only = T)
    path_to_folder <- paste0("C:/Users/", Sys.info()["user"], download_folder)
    setwd(path_to_folder)
    xls_file <- dir()
    sheet_name <- XLConnect::getSheets(XLConnect::loadWorkbook(xls_file))
    sheet_title <- vector()
    UnemploymentByAge <- list()
    for (iii in 1:2) {
        buf0 <- readWorksheetFromFile(xls_file, sheet = sheet_name[iii], check.names = F, header = F)
        buf0 <- f_fill_cells_column_direction(df = buf0, row = 6, below_check = F)
        buf0 <- f_fill_cells_column_direction(df = buf0, row = 7, below_check = F)
        sheet_title[iii] <- zen2han(buf0[1, 5])
        colnames(buf0) <- sapply(gsub(":na", "", gsub("male", "男", gsub("female", "女", gsub("both sexes", "男女計", paste0(buf0[6, ], ":", buf0[7, ], ":", buf0[8, ]), ignore.case = T), ignore.case = T), ignore.case = T), ignore.case = T), zen2han)
        buf1 <- buf0[!is.na(as.numeric(gsub("月", "", buf0[, 2]))), ]
        buf1[, 1] <- seq(as.Date(paste0(as.numeric(gsub("昭和|年", "", buf1[1, 1])) + 1925, "-", gsub("月", "", buf1[1, 2]), "-1")), by = "+1 month", length.out = nrow(buf1))
        colnames(buf1)[1] <- "Date"
        buf2 <- buf1[, -grep("na", colnames(buf1), ignore.case = T)]
        buf2[, -1] <- apply(buf2[, -1], 2, function(x) as.numeric(gsub("<|>", "", x)))
        row.names(buf2) <- NULL
        colnames(buf2)[-1] <- gsub("\\s", "", paste0("完全失業率(%):", sheet_name[iii], ":", colnames(buf2)[-1]))
        buf3 <- buf2[apply(buf2[, -1], 1, function(x) !all(is.na(x))), ]
        UnemploymentByAge[[iii]] <- buf3
        print(tail(UnemploymentByAge[[iii]][, c(1:2)], 10))
    }
    return_list <- list(UnemploymentByAge = UnemploymentByAge, sheet_title = sheet_title)
    return(return_list)
}

Asset Management Consulting Corporation