6

I want convert the following the column of the dataframe in to 00:00:00 (HH:MM:SS) and class of the output should be the "POSIXct" format.

This is not an exact duplicate of as.POSIXct with datetimes including midnight because these times do not include midnight.

Here Column of the dataframe.

> 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"

I tired the following code,it's not solve the my problem

  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"

And i need class of the output should be in "POSIXct" "POSIXt"

So i am applying the as.POSIXct

and here missing the 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"

Required format is

[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"

And class should be "POSIXct" "POSIXt"

Dataset

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

Thank you.

De Novo
  • 7,120
  • 1
  • 23
  • 39
dondapati
  • 829
  • 6
  • 18
  • format(gg,'%Y-%m-%d 00:00:00 IST') – vsb Mar 08 '18 at 11:29
  • 1
    This is a different question from the one it's marked as an exact duplicate of. This question is asking how to make a time that is NOT midnight appear as if it is midnight, i.e., to round it to the day. The other question is asking how to force a time that IS midnight to show the 00:00:00 H:M:S information. That answer can be used as part of the solution for this question, but it's a different question, and doesn't have any rounding component. – De Novo Mar 08 '18 at 13:20
  • When you understand why this is a duplicate your problem will be solved. – Ista Mar 08 '18 at 23:08
  • Agree to disagree. `round(gg, units = "days")` remains a POSIX object, and has been converted from a date that isn't midnight to a date that is midnight. `format()` converts it to character. Just because the desired behavior in a post can be reproduced with the answer to another post, does not make the question an EXACT duplicate. – De Novo Mar 08 '18 at 23:21
  • 1
    Both questions ask essentially "why doesn't `as.POSIXct("2018-01-16 00:00:00")` display a time?". – Ista Mar 09 '18 at 01:19
  • Yes, and they are not exact duplicates – De Novo Mar 09 '18 at 16:00

1 Answers1

5

There is a round method for POSIX.ct

round(gg, units = "days")

To get it to display the 00:00:00

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

Round has the benefit of being locale independent.

De Novo
  • 7,120
  • 1
  • 23
  • 39