Function

コメント・参照等
  • 労働力人口、完全失業率等ファイル(総務省)を読み込み整形する関数

FUN = f_read_RoudouryokuChousa.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(pattern = "\\.xls")
    sheet_names <- XLConnect::getSheets(XLConnect::loadWorkbook(xls_file))
    obj_sheet <- c("季節調整値", "原数値")
    for (iii in seq(obj_sheet)) {
        buf0 <- readWorksheetFromFile(file = xls_file, sheet = which(grepl(paste0("^", obj_sheet[iii], "$"), sheet_names)), check.names = F, header = F)
        sheet_unit <- zen2han(unique(buf0[4, 5], buf0[4, 14]))
        rrr <- 6
        buf0[, 2] <- as.numeric(gsub("月", "", buf0[, 2]))
        buf0 <- f_fill_cells_column_direction(df = buf0, row = rrr, below_check = F)
        buf0[rrr, -c(1:4)] <- sapply(buf0[rrr, -c(1:4)], zen2han)
        col_ratio <- grep("%", buf0[rrr, ])
        buf0[rrr, -col_ratio] <- paste0(buf0[rrr, -col_ratio], sheet_unit)
        colnames(buf0) <- sapply(paste0(buf0[6, ], ":", obj_sheet[iii], ":", buf0[8, ]), zen2han)
        buf1 <- buf0[!is.na(as.numeric(buf0[, 2])), ]
        buf1[1, 1] <- as.numeric(gsub("[^0-9]+([0-9]+).+", "\\1", buf1[1, 1])) + 1925
        buf1[, 1] <- seq(as.Date(paste0(buf1[1, 1], "-", buf1[1, 2], "-1")), by = "+1 month", length.out = nrow(buf1))
        buf2 <- buf1[, -c(2, 3, 4)]
        buf2[, -1] <- apply(buf2[, -1, drop = F], 2, function(x) as.numeric(gsub("\\(|\\)|<|>", "", x)))
        row.names(buf2) <- NULL
        colnames(buf2)[1] <- "Date"
        colnames(buf2)[-1] <- paste0("労働力調査:", colnames(buf2)[-1])
        if (obj_sheet[iii] == "季節調整値") {
            assign("RoudouryokuChousa_sa", buf2)
        }
        else {
            assign("RoudouryokuChousa_nsa", buf2)
        }
    }
    return_list <- list(RoudouryokuChousa_sa = RoudouryokuChousa_sa, RoudouryokuChousa_nsa = RoudouryokuChousa_nsa)
    return(return_list)
}