如何在时间对象的向量中找到最近的时间段?
我有一个特定的时间段,并且它与我的数据框中的任何时间对象列都不匹配。 例如,X(在下一节中)是我的TimeObject列。我想找出最近的时间(在下一节中)。
x<-as.POSIXct(c("2018-10-26 00:00:43 NZDT" ,"2018-10-26 00:01:13 NZDT" ,"2018-10-26 00:01:43 NZDT" ,"2018-10-26 00:02:13 NZDT", "2018-10-26 00:02:43 NZDT" ,"2018-10-26 00:03:13 NZDT", "2018-10-26 00:03:43 NZDT", "2018-10-26 00:06:13 NZDT","2018-10-26 00:06:43 NZDT" ,"2018-10-26 00:07:13 NZDT") ,origin= "1970-01-01", tz=Sys.timezone())
y<-as.POSIXct("2018-10-26 00:01:12 NZDT", ,origin= "1970-01-01", tz=Sys.timezone())
我知道我可以简单地用y减去x的每个值并找到最小值。但是有直接功能吗?
I have a particular time period, and it dosen't match with any of the timeObject column within my dataframe.
For example, say x (in the following section) is my column of timeObject. I want to find out the nearest time to y (in the following section).
x<-as.POSIXct(c("2018-10-26 00:00:43 NZDT" ,"2018-10-26 00:01:13 NZDT" ,"2018-10-26 00:01:43 NZDT" ,"2018-10-26 00:02:13 NZDT", "2018-10-26 00:02:43 NZDT" ,"2018-10-26 00:03:13 NZDT", "2018-10-26 00:03:43 NZDT", "2018-10-26 00:06:13 NZDT","2018-10-26 00:06:43 NZDT" ,"2018-10-26 00:07:13 NZDT") ,origin= "1970-01-01", tz=Sys.timezone())
y<-as.POSIXct("2018-10-26 00:01:12 NZDT", ,origin= "1970-01-01", tz=Sys.timezone())
I know I can simply subtract every value of x with y and find the minimum value. But is there a direct function for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以简单地尝试
You may simply try