Function

コメント・参照等
  • 求人倍率ファイル(厚生労働省)を読み込み整形する関数

FUN = f_read_JobOpeningsEmployee.r
function (download_folder) 
{
    lapply(c("XLConnect", "Nippon", "lubridate"), require, character.only = T)
    path_to_folder <- paste0("C:/Users/", Sys.info()["user"], download_folder)
    setwd(path_to_folder)
    xls_file <- dir()
    for (xxx in seq(xls_file)) {
        sheet_name <- XLConnect::getSheets(XLConnect::loadWorkbook(xls_file[xxx]))
        for (sheet in seq(sheet_name)) {
            buf0 <- readWorksheetFromFile(file = xls_file[xxx], sheet = sheet, check.names = F, header = F)
            colnames(buf0) <- sapply(buf0[4, ], function(x) zen2han(gsub("[[:space:]]", "", x)))
            sheetTitle <- sapply(grep("実数|季節調整値", colnames(buf0)), function(x) zen2han(buf0[1, x]))
            buf1 <- buf0[-c(1:4), ]
            datadf <- list()
            datadf[[1]] <- buf1[, c(1:(grep("季節調整値", colnames(buf1)) - 1))]
            datadf[[2]] <- buf1[, c(grep("季節調整値", colnames(buf1)):ncol(buf1))]
            for (iii in 1:2) {
                tmp0 <- datadf[[iii]]
                print(head(tmp0))
                yyyy <- 1900 + as.numeric(gsub("年", "", tmp0[1, 2]))
                Value <- as.numeric(as.vector(t(tmp0[, grep("[0-9]月$", colnames(tmp0))])))
                Date <- seq(from = as.Date(paste0(yyyy, "-1-1")), by = "+1 month", length.out = length(Value))
                tmp1 <- data.frame(Date, Value, stringsAsFactors = F, check.names = F)
                colnames(tmp1)[2] <- sheetTitle[iii]
                if (iii == 1) {
                  eachdata <- tmp1
                }
                else {
                  eachdata <- merge(eachdata, tmp1)
                }
            }
            if (sheet == 1) {
                alldata <- eachdata
            }
            else {
                alldata <- merge(alldata, eachdata)
            }
        }
        finaldata <- alldata[!apply(alldata[, -1], 1, function(x) all(is.na(x))), ]
        return(finaldata)
    }
}

Asset Management Consulting Corporation