【问题标题】:how to convert number into date and timestamp in R? [duplicate]如何将数字转换为R中的日期和时间戳? [复制]
【发布时间】:2021-03-22 10:06:50
【问题描述】:

这是一个示例。在日期和时间戳中查找结果。

dput(头(df, 44245, 44242, 44245.66, 44231)

结果示例:- 2021-02-18 16:08:13

【问题讨论】:

    标签: r


    【解决方案1】:

    如果数字来自 Excel,您可以使用 as.POSIXct:

    as.POSIXct(c(44245, 44242, 44245.66, 44231) * 86400, origin = "1899-12-30", tz="GMT")
    #[1] "2021-02-18 00:00:00 GMT" "2021-02-15 00:00:00 GMT"
    #[3] "2021-02-18 15:50:24 GMT" "2021-02-04 00:00:00 GMT"
    

    【讨论】:

      【解决方案2】:

      假设这些是从 Excel 导入的数据时间:

      library( openxlsx )
      strings <- c( 44245, 44242, 44245.66, 44231 )
      openxlsx::convertToDateTime( strings )
      # [1] "2021-02-18 00:00:00 CET" "2021-02-15 00:00:00 CET" "2021-02-18 15:50:24 CET" "2021-02-04 00:00:00 CET"
      

      【讨论】:

        【解决方案3】:

        我们也可以使用janitor中的excel_numeric_to_date

        library(janitor)
        excel_numeric_to_date(v1, include_time = TRUE)
        #[1] "2021-02-18 00:00:00 CST" "2021-02-15 00:00:00 CST" "2021-02-18 15:50:24 CST" "2021-02-04 00:00:00 CST"
        

        数据

        v1 <- c( 44245, 44242, 44245.66, 44231 )
        

        【讨论】:

          猜你喜欢
          • 2016-05-17
          • 1970-01-01
          • 1970-01-01
          • 2016-11-26
          • 1970-01-01
          • 2019-11-29
          • 2019-09-06
          • 2018-01-06
          • 1970-01-01
          相关资源
          最近更新 更多