Rindrics / lucifer

ネ申エクセルと戦うためのRパッケージ
GNU General Public License v3.0
3 stars 0 forks source link

make_asciiにnumericが渡ったらスルー #99

Closed akimanabe closed 5 years ago

akimanabe commented 5 years ago

以前SlackにUPしていただいた佐賀県データで勉強してます。 make_ascii以前までは動きますが、make_asciiで

 >Error in Encoding(s) : a character vector argument expected

と言われて怒られました。

ちなみに佐賀県の場合年度が西暦と和暦の二つ存在していて、うまくRunしなかったので、この勉強の場合だけ強制的に和暦のColumnを削除してます。

よろしくお願いします。

maaji <- rebel(fname_saga, sheet_regex = "マアジ.+",
           row_type = "fisY",
           col_type = list(regex = ".+月",
                           newname = "month",
                           varname = "catch"),
           cluster = list(dir = "row",
                          pos = 1,
                          regex = "年度",
                          offset = c(0, 0),
                          ends = list(row = "2016", col = "3月")))%>%
  make_ascii(col = "month", numerize = TRUE) %>%
  dplyr::mutate(year = as.numeric(年度),
            catch = as.numeric(catch))
Rindrics commented 5 years ago

What I did

fmtcatch.saga <- function(fname, year) {
  fname %>%
    lucifer::rebel(sheet_regex = "^(?!.*集計区分概念図).*$",
                   row_type = "fisY",
                   col_type = list(regex = ".+月",
                                   newname = "month",
                                   varname = "catch"),
                   cluster = list(dir = "row",
                                  pos = 1,
                                  regex = "年度",
                                  offset = c(0, 0),
                                  ends = list(row = as.character(year),
                                              col = "3月"))) %>%
    lucifer::make_ascii(col = "month", numerize = TRUE) %>% # 既にnumericになっている列に対してmake_asciiしようとすると怒られる.
    dplyr::rename(year = "年度",
                  jyear = "年度.1") %>%
    dplyr::mutate(year = as.integer(year),
                  month = as.integer(month),
                  catch = as.numeric(catch),
                  sheet_copy = sheet,
                  catch_ton = catch / 1000,
                  period = cut(year, breaks = seq(1960, 2020, 10),
                               dig.lab = 4)) %>%
    tidyr::separate(sheet_copy, c("spcs", "fishery"), sep = "(") %>%
    dplyr::mutate(fishery = stringr::str_remove(fishery, ")")) %>%
    tibble::as_tibble()
}

Error msg

>Error in Encoding(s) : a character vector argument expected

Cause

make_ascii() accepts only character class objects

What I will do

if (is.numeric(x)) {
  message("Already ascii")
  return(x)
}