首页 > 解决方案 > 使用 chron 包在 R 中格式化数据

问题描述

目前我正在尝试将字符串转换为时间格式。例如,我的字符串如下所示:time <- '12:00'。我已经尝试使用 chron-Package。我的代码如下所示:

time <- paste(time,':00', sep = '') time <- times(time)

而不是得到像“12:00:00”这样的值,函数times()总是将对象时间转换为“0.5”我使用了错误的方法吗?

问候

标签: rchron

解决方案


您的代码有效。如果您检查'class()',它是“次”。但是,如果您想要其他方式,请尝试:

time <- '12:00:00'
newtime<-as.POSIXlt(time, format = "%H:%M:%S") # The whole date with time
t <- strftime(newtime, format="%H:%M:%S") # To extract the time part
t
#[1] "12:00:00"

干杯!


推荐阅读