indicateing to which interval a date belongs

拟墨画扇 提交于 2019-12-25 21:23:03

问题


4/1/2014 0:11   40.769  -73.9549    B02512|
4/1/2014 0:17   40.7267 -74.0345    B02512|
4/1/2014 0:21   40.7316 -73.9873    B02512|
4/1/2014 0:28   40.7588 -73.9776    B02512|
4/1/2014 0:33   40.7594 -73.9722    B02512|
4/1/2014 0:33   40.7383 -74.0403    B02512|

and i created a time interval objet that lokks like this C--2014-04-11 00:15:00 UTC 2014-04-11 00:15:00 UTC(part of it ) what i want to do is to to add a column to the raw data, that indicates for each row, for which time interval (which is represented by a single timepoint at the beginning of the time interval) it belongs

so my data would look like this :

4/1/2014 0:11   40.769  -73.9549    B02512  4/1/2014 0:00|
4/1/2014 0:17   40.7267 -74.0345    B02512  4/1/2014 0:15|

how can i do that ?


回答1:


Using lubridate's floor_date function:

df$interv<-lubridate::floor_date(df$time, unit="15 minutes")

(This assumes that your time column is some recognized date-time format (e.g., POSIXt), which it doesn't seem to be from your sample's formatting.)

For example:

a<-Sys.time()
a
[1] "2019-01-08 09:22:22 EST"
floor_date(a,unit="15 minutes")
[1] "2019-01-08 09:15:00 EST"


来源:https://stackoverflow.com/questions/54093675/indicateing-to-which-interval-a-date-belongs

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