将posixct时间格式转换为00:00:00

时间:2018-03-08 11:05:25

标签: r

我希望将以下数据帧的列转换为00:00:00(HH:MM:SS) 和class of the output should be the "POSIXct" format

这与as.POSIXct with datetimes including midnight不完全相同,因为这些时间不包括午夜。

数据框的列。

> gg
[1] "2018-01-16 10:29:00 IST" "2018-01-16 10:29:00 IST"
[3] "2018-01-16 10:29:00 IST" "2018-01-16 10:29:00 IST"
[5] "2018-01-16 10:29:00 IST" "2018-01-16 10:29:00 IST"

我厌倦了以下代码,但这并没有解决我的问题

  paste(as.Date(gg, format="%Y-%m-%d"),"00:00:00")

[1] "2018-01-16 00:00:00" "2018-01-16 00:00:00" "2018-01-16 00:00:00"
[4] "2018-01-16 00:00:00" "2018-01-16 00:00:00" "2018-01-16 00:00:00"

我需要输出的类应该在"POSIXct" "POSIXt"

所以我正在应用as.POSIXct

此处缺少00:00:00

as.POSIXct(paste(as.Date(gg, format="%Y-%m-%d"),"00:00:00"))

[1] "2018-01-16 IST" "2018-01-16 IST" "2018-01-16 IST" "2018-01-16 IST"
[5] "2018-01-16 IST" "2018-01-16 IST"

所需格式为

[1] "2018-01-16 00:00:00 IST" "2018-01-16 00:00:00 IST"
[3] "2018-01-16 00:00:00 IST" "2018-01-16 00:00:00 IST"
[5] "2018-01-16 00:00:00 IST" "2018-01-16 00:00:00 IST"

课程应为"POSIXct" "POSIXt"

数据集

> dput(gg)
structure(c(1516078740, 1516078740, 1516078740, 1516078740, 1516078740, 
1516078740), class = c("POSIXct", "POSIXt"), tzone = "")

谢谢。

1 个答案:

答案 0 :(得分:1)

POSIX.ct

有一种循环方法
round(gg, units = "days")

让它显示00:00:00

format(round(gg, units = "day"), '%Y-%m-%d %M:%H:%S')

Round具有独立于区域设置的优点。