【问题标题】:Time-efficient way to convert column to as.Date column将列转换为 as.Date 列的省时方法
【发布时间】:2021-04-15 16:20:20
【问题描述】:

在没有事务时间的情况下,将“事务”列转换为 as.Date() 格式最省时的方法是什么? 谢谢!

ID <- c(10,27,35)
Transaction <- c("15-JAN-20 12.00.00","10-MAR-19 13.00.00","01-JAN-20 13.30.00")

df <- data.frame(ID, Transaction)

【问题讨论】:

    标签: r dplyr lubridate stringr


    【解决方案1】:

    as.Date 最后会忽略垃圾。没有使用任何包。

    transform(df, date = as.Date(Transaction, "%d-%b-%y"))
    

    给予:

      ID        Transaction       date
    1 10 15-JAN-20 12.00.00 2020-01-15
    2 27 10-MAR-19 13.00.00 2019-03-10
    3 35 01-JAN-20 13.30.00 2020-01-01
    

    【讨论】:

      【解决方案2】:

      此解决方案也适用于 lubridate 包:

      library(dplyr)
      library(lubridate)
      
      df %>%
        mutate(date = date(dmy_hms(Transaction)))
      
        ID        Transaction       date
      1 10 15-JAN-20 12.00.00 2020-01-15
      2 27 10-MAR-19 13.00.00 2019-03-10
      3 35 01-JAN-20 13.30.00 2020-01-01
      
      

      【讨论】:

        猜你喜欢
        • 2021-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-16
        相关资源
        最近更新 更多