如何从datetime创建日期(使用lubridate)? [英] How to create date from datetime (using lubridate)?

查看:54
本文介绍了如何从datetime创建日期(使用lubridate)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我创建了一个包含日期和时间的变量:

Assume I have created a variable containing date and time:

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

如何创建另一个仅具有日期的变量?也就是说,我应该怎么做才能将a的值转换为与b的值相同,其中b

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

推荐答案

您可以只使用round_date

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

编辑

不过,您确实需要注意舍入时间.为了完全等效,您需要使用floor_date.

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

这篇关于如何从datetime创建日期(使用lubridate)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆