How to create date from datetime (using lubridate)?

天涯浪子 提交于 2019-12-30 18:58:10

问题


Assume I have created a variable containing date and time:

a <- ymd_hms("2014-01-01 12:23:34")

How do I create another variable that only has the date? That is, what should I do to transform a's value to be identical to b's value where b is

b <- ymd("2014-01-01")

回答1:


You can just use round_date

round_date(a, "day")
[1] "2014-01-02 UTC"

EDIT

You do need to be careful with rounding the time though. For complete equivalence here you would need to use floor_date.

identical(ymd("2014-01-01"), floor_date(a, "day"))
[1] TRUE


来源:https://stackoverflow.com/questions/28729429/how-to-create-date-from-datetime-using-lubridate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!