r - 使用 chron 包在 R 中格式化数据
问题描述
目前我正在尝试将字符串转换为时间格式。例如,我的字符串如下所示:time <- '12:00'
。我已经尝试使用 chron-Package。我的代码如下所示:
time <- paste(time,':00', sep = '') time <- times(time)
而不是得到像“12:00:00”这样的值,函数times()总是将对象时间转换为“0.5”我使用了错误的方法吗?
问候
解决方案
您的代码有效。如果您检查'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"
干杯!
推荐阅读
- java - 有没有办法循环输入请求,直到用户满足 BigInteger 中的条件?
- python - 使用 lambda 中的 pandas 从 s3 读取 excel 文件并转换为 csv
- java - 使用正则表达式的 jsonAssert 嵌套 Json 验证
- docker - 为什么我无法连接到我的 socket.io 服务器?
- javafx - 如何定义胶子应用程序版本
- python - 解析json列表
- node.js - undefined 不可迭代(无法读取属性 Symbol(Symbol.iterator))
- java - 回到之前的活动
- python - 如何绘制方向梯度的直方图?
- javascript - highcharts:向 x 范围图表添加标记