【问题标题】:R strftime() only preserving date, not timeR strftime() 仅保留日期,不保留时间
【发布时间】:2015-10-09 23:28:53
【问题描述】:

我的 R 脚本读取两列的 CSV 数据,其中列是时间戳和标量值,其中:

data = read.csv(dataFilePath, colClasses=c("charDate", "numeric"))

我的时间戳被格式化,例如“5/14/14 13:14”。 charDate 类被定义为转换时间戳:

setClass("charDate")
setAs("character", "charDate", function(from) strftime(from))

我正在运行的数据文件是here。它们都按预期读入,在 R 数据帧data 中表示为例如“2014-05-14 13:14:00”,realTweets/ 目录中的除外。为什么?对于 realTweets/,data 中的时间戳看起来像例如“2014-05-14”,时间信息未保存。

【问题讨论】:

  • @RichardScriven 运行上述代码后时间被删除。

标签: r csv timestamp


【解决方案1】:

问题在于strftime()。正如this answer 中所解释的,strftime() 只是包装了as.POSIXlt()。使用as.POSIXlt() 直接解决了我的问题:

setAs("character", "charDate", function(from) as.POSIXlt(from))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 2019-07-28
    • 2022-01-13
    • 2022-07-02
    相关资源
    最近更新 更多