R中两个日期和时间列的频率计数

时间:2019-12-17 17:47:23

标签: r dataframe

我有一个114209数据帧(df),其中有两列:

Date            Time 
2017-05-01     23H 0M 0S
2017-05-01     23H 0M 0S
2017-05-01     23H 30M 0S
2017-05-01     23H 30M 0S
2017-05-02     23H 0M 0S
2017-05-02     22H 0M 0S
2017-05-02     23H 0M 0S

> str(m)
'data.frame':   114209 obs. of  2 variables:
 $ DATAS         : Date, format: "2017-05-01" "2017-05-01" ...
 $ Time_Intervals:Formal class 'Period' [package "lubridate"] with 6 slots
  .. ..@ .Data : num  0 0 0 0 0 0 0 0 0 0 ...
  .. ..@ year  : num  0 0 0 0 0 0 0 0 0 0 ...
  .. ..@ month : num  0 0 0 0 0 0 0 0 0 0 ...
  .. ..@ day   : num  0 0 0 0 0 0 0 0 0 0 ...
  .. ..@ hour  : num  1 1 1 2 3 7 7 8 8 8 ...
  .. ..@ minute: num  0 0 0 30 0 30 30 30 30 30 ...

我需要计算两列的频率并以这种格式获取结果:

Date            Time         Freq
2017-05-01     23H 0M 0S       2
2017-05-01     23H 30M 0S      2
2017-05-02     23H 0M 0S       2
2017-05-02     22H 0M 0S       1

我认为问题在于我的数据是日期和时间。

我已经尝试了几种解决类似问题的方法,但是没有任何效果。 -> as.data.frame(table(df))-结果是一个全为0的表

> counts <- ddply(m, .(m$DATAS, m$Time_Intervals), nrow)
> names(counts) <- c("Datas", "Time", "Freq")
> counts
         Datas      Time Freq
1   2017-05-01  1H 0M 0S  125
2   2017-05-02    30M 0S  129
3   2017-05-03    30M 0S  117

有什么想法吗?

0 个答案:

没有答案