【问题标题】:Extracting hour, minute and second from timestamp [duplicate]从时间戳中提取小时、分钟和秒 [重复]
【发布时间】:2019-01-03 19:46:22
【问题描述】:

如何从 R 中的数据框中的时间戳中提取小时、分钟和秒?我的代码是

lubridate::ymd_hms(d1$timestamp, tz = "UTC")
d2= lubridate::ymd_hms(d1$timestamp, tz = "UTC")
d3= lubridate::hms(d2)

【问题讨论】:

    标签: r datetime datetime-format


    【解决方案1】:

    试试 lubridate 包。

    > library(libridate)
    > k <- ymd_hms('2018-11-08T07:41:55.921Z')
    > minute(k)
    [1] 41
    > second(k)
    [1] 55.921
    

    【讨论】:

    • 非常感谢您的帮助@Joseph Clark McIntyre
    【解决方案2】:

    先转换为POSIXct

    x <- "2018-11-08T07:41:55.921Z"
    x.ct <- as.POSIXct(x, format="%Y-%m-%dT%H:%M:%OS")
    

    然后提取

    format(x.ct, "%H") # hours
    # [1] "07"
    
    format(x.ct, "%M") # minutes
    # [1] "41"
    
    format(x.ct, "%S") # seconds (truncated integer)
    # [1] "55"
    
    format(x.ct, "%OS3") # seconds (three decimal places)
    # [1] "55.921"
    

    POSIXlt

    x.lt <- as.POSIXlt(x, format="%Y-%m-%dT%H:%M:%OS")
    
    x.lt$hour
    # 7
    
    x.lt$min
    # 41
    
    x.lt$sec
    # 55.921
    

    【讨论】:

    • 非常感谢您的帮助@AkselA
    • @user5576922:请记住,您可以accept 找到您认为最有帮助的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 2017-08-01
    • 2013-10-08
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    相关资源
    最近更新 更多