| 2015/1/1 0:35 |
| 2015/1/1 0:35 |
| 2015/1/1 0:35 |
| 2015/1/1 0:36 |
| 2015/1/1 0:36 |
| 2015/1/1 0:36 |
| 2015/1/1 0:36 |
| 2015/1/1 0:36 |
| 2015/1/1 0:34 |
| 2015/1/1 0:34 |
| 2015/1/1 0:34 |
| 2015/1/1 0:34 |
数据集样本如上,比如2015/1/1 0:35为2015年1月1日 0时35分,我现在想要得到的是>=34分钟,<=35分钟的数据有多少个。(数据量也比较大,我提取了500组数据进行试验)
(参考http://stackoverflow.com/questions/11853524/comparing-time-portion-of-posixct-in-r)
我自己做的过程如下:
trip_date <- read.csv("D:\\data\\date111.csv",header=T) #导入数据
t <- as.POSIXct(trip_date $pickup_datetime,'%Y/%m/%d %H:%M',tz='UTC') #将数据转为as.POSIXct格式,pickup_datetime为字段名
主要想用参考网址中的这个计算方法,但是如下处理会返回全部数据
t_34 <- subset(t,
(as.numeric(format(t, "%H")) >= 0 & as.numeric(format(t, "%M")) >= 34) |
(as.numeric(format(t, "%H")) < 1 & as.numeric(format(t, "%M")) <= 35))
我以为是对象的类属性的问题,因为参考网址中的数据结构为data.frame,而此时的t为POSIXt,因此我对其进行转化:
t <- as.data.frame(t)
再次使用参考的方法,仍然返回全部值:
t_34 <- subset(t,
(as.numeric(format(t, "%H")) >= 0 & as.numeric(format(t, "%M")) >= 34) |
(as.numeric(format(t, "%H")) < 1 & as.numeric(format(t, "%M")) <= 35))
我也尝试也网址中的另外一种方法,也遇到了各种问题,所以想请教下统计这类时间有没有比较好的方法或者上面的方法哪里出了问题??