Function

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

FUN = f_read_EmploymentRateByAge.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 <- 1
    buf0 <- readWorksheetFromFile(file = xls_file, sheet = sheet, check.names = F, header = F)
    sheet_title <- zen2han(buf0[1, 5])
    print(sheet_title)
    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)
    colnames(buf0) <- sapply(gsub(":na|^\\s*", "", 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)))
    colnames(buf2)[-1] <- paste0(sheet_name[sheet], ":", colnames(buf2)[-1])
    colnames(buf2)[-1] <- paste0(gsub(".+?([^[:space:]]+率).+", "\\1", sheet_title), ":", colnames(buf2)[-1])
    EmploymentRateByAge <- buf2[apply(buf2[, -1], 1, function(x) !all(is.na(x))), ]
    row.names(EmploymentRateByAge) <- NULL
    return(EmploymentRateByAge)
}

Asset Management Consulting Corporation