【问题标题】:How to apply excel_numeric_to_date inside a customized function?如何在自定义函数中应用 excel_numeric_to_date?
【发布时间】:2021-06-07 20:00:25
【问题描述】:

我正在尝试将自定义函数应用于 Excel 导入日期列。

我应用下一个函数:

library(janitor)

fun_excel_date <- function(x){
  if(is.numeric(x)){
  excel_numeric_to_date(as.numeric(
    as.character(x)
  ), date_system = "modern")}
  
  else {
    return(NA)
  }
  
}

do.call(rbind, lapply(some_dummy_dates$date, fun_excel_date))

控制台输出:

#     [,1]
#[1,] 3967
#[2,] 7783
#[3,] 6028
#[4,] 4479

当应用于一个元素时,该函数可以正常工作,例如excel_numeric_to_date(as.numeric(as.character(29536)), date_system = "modern"),因为它返回"1980-11-11"。

但是,当应用于整个列时,该函数会返回一个无法解释的数字输出。 即使在函数内部将x 参数更改为x &lt;- as.Date(x, origin="1899-12-30"),问题仍然存在。

数据

some_dummy_dates <-structure(list(date = c(29536, 33352, 31597, 30048)), class = "data.frame", row.names = c(NA, 
-4L))

我是否在函数内部遗漏了什么? 还有其他方法吗?

【问题讨论】:

    标签: r excel function date janitor


    【解决方案1】:

    Date 类在内部存储为整数。因此,如果我们想避免强制转换为整数,请使用c

    do.call(c, lapply(some_dummy_dates$date, fun_excel_date))
    #[1] "1980-11-11" "1991-04-24" "1986-07-04" "1982-04-07"
    

    此外,转换为 matrix 将删除 Date 类并将其更改为 integer

    matrix(do.call(c, lapply(some_dummy_dates$date, fun_excel_date)))
    #     [,1]
    #[1,] 3967
    #[2,] 7783
    #[3,] 6028
    #[4,] 4479
    

    如果我们想将它存储在一个对象中,要么存储为vector,要么创建一个可以有属性的data.frame/tibble/data.table

    data.frame(Date = do.call(c, lapply(some_dummy_dates$date, fun_excel_date)))
    #     Date
    #1 1980-11-11
    #2 1991-04-24
    #3 1986-07-04
    #4 1982-04-07
    

    或者使用rowwise的函数

    library(dplyr)
    some_dummy_dates %>%
       rowwise %>%
       mutate(Date = fun_excel_date(date)) %>%
       ungroup
    

    OP 的函数使用未矢量化的if/else,这就是它只需要一个元素并对其进行转换的原因。这可以通过同时检查列是否为numeric(在across 内)然后通过应用excel_numeric_to_date 创建/修改列来进行矢量化

    library(janitor)
    some_dummy_dates %>% 
        mutate(across(where(is.numeric), 
           ~ excel_numeric_to_date(., date_system = 'modern'), .names = "{.col}_new"))
    #   date   date_new
    #1 29536 1980-11-11
    #2 33352 1991-04-24
    #3 31597 1986-07-04
    #4 30048 1982-04-07
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多