舍入具有基本R功能的POSIX日期(POSIXct) [英] Round a POSIX date (POSIXct) with base R functionality

查看:119
本文介绍了舍入具有基本R功能的POSIX日期(POSIXct)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在玩很多日期和时间,我正在建立一个包。

I'm currently playing around a lot with dates and times for a package I'm building.

绊倒这个邮箱再次提醒我,在转到contrib包之前,检查一些可以使用基本R功能的东西,通常不是一个坏主意。

Stumbling across this post reminded me again that it's generally not a bad idea to check out if something can be done with basic R features before turning to contrib packages.

因此,是否可以使用基本R功能来舍入类 POSIXct 的日期?

Thus, is it possible to round a date of class POSIXct with base R functionality?

我查了

methods(round)

哪个只给了我

[1] round.Date      round.timeDate*

   Non-visible functions are asterisked

这是我想做的(伪代码)

This is what I'd like to do (Pseudo Code)

x <- as.POSIXct(Sys.time())
[1] "2012-07-04 10:33:55 CEST"

round(x, atom="minute")
[1] "2012-07-04 10:34:00 CEST"

round(x, atom="hour")
[1] "2012-07-04 11:00:00 CEST"

round(x, atom="day")
[1] "2012-07-04 CEST"

我知道这可以通过 timeDate lubridate 等,但我想保持包的依赖下来了所以在继续查看相关软件包的源代码之前,我以为我会问是否有人已经做过这样的事情了。

I know this can be done with timeDate, lubridate etc., but I'd like to keep package dependencies down. So before going ahead and checking out the source code of the respective packages, I thought I'd ask if someone has already done something like this.

推荐答案

base round.POSIXt 来做到这一点。不知道为什么没有提出方法

base has round.POSIXt to do this. Not sure why it doesn't come up with methods.

x <- as.POSIXct(Sys.time())
x
[1] "2012-07-04 10:01:08 BST"
round(x,"mins")
[1] "2012-07-04 10:01:00 BST"
round(x,"hours")
[1] "2012-07-04 10:00:00 BST"
round(x,"days")
[1] "2012-07-04"

这篇关于舍入具有基本R功能的POSIX日期(POSIXct)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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