计算r中的每月频率

时间:2016-06-20 09:31:28

标签: r dataframe time-series

我有一个大致看起来像这样的数据框(在问题的最后也是dput):

     dates    var1    var2    var3
1997-01-15       0    -0.5    -1.0
1997-01-17       0   -0.42   -0.85
1997-02-03    0.23       0       0
1997-02-09    0.46       0       0

我需要按月汇总这些数据。但我还需要这些数据的每月频率。现在,原始月度聚合不是问题,例如:

myFrame$month <- as.Date(cut(frame$dates, breaks = "month"))
as.data.frame(aggregate(var1 ~ month, frame, mean))
所有变量都是

等等(虽然我不确定这是否是最有效的方法,因为我必须分别对每个变量执行此操作) - 这给了我一个如下所示的框架:

     month    var1    var2    var3
1997-01-01       0   -0.46  -0.925
1997-02-01   0.345       0       0

但是,由于我还需要所有变量的月频率,我需要一个如下所示的数据帧:

     month    var1    var2    var3    freq_v1    freq_v2    freq_v3
1997-01-01       0   -0.46  -0.925          0          2          2
1997-02-01   0.345       0       0          2          0          0

这就是我不知道该怎么办。 谢谢!

DPUT: dput(frame) structure(list(dates = structure(c(9876, 9877, 9878, 9879, 9880, 9881, 9882, 9883, 9884, 9885, 9886, 9887, 9888, 9889, 9890, 9891, 9892, 9893, 9894, 9895, 9896, 9897, 9898, 9899, 9900, 9901, 9902 ), class = "Date"), var1 = c(0, -0.461538461538462, 0, -0.384615384615385, 0, -0.307692307692308, 0, -0.230769230769231, 0, -0.153846153846154, 0, -0.0769230769230769, 0, 0, 0, 0.076923076923077, 0, 0.153846153846154, 0, 0.230769230769231, 0, 0.307692307692308, 0, 0.384615384615385, 0, 0.461538461538462, 0), var2 = c(-0.5, 0, -0.423076923076923, 0, -0.346153846153846, 0, -0.269230769230769, 0, -0.192307692307692, 0, -0.115384615384615, 0, -0.0384615384615384, 0, 0.0384615384615385, 0, 0.115384615384615, 0, 0.192307692307692, 0, 0.269230769230769, 0, 0.346153846153846, 0, 0.423076923076923, 0, 0.5), var3 = c(-1, 0, -0.846153846153846, 0, -0.692307692307692, 0, -0.538461538461538, 0, -0.384615384615385, 0, -0.230769230769231, 0, -0.0769230769230769, 0, 0.0769230769230771, 0, 0.230769230769231, 0, 0.384615384615385, 0, 0.538461538461539, 0, 0.692307692307693, 0, 0.846153846153846, 0, 1), month = structure(c(9862, 9862, 9862, 9862, 9862, 9862, 9862, 9862, 9862, 9862, 9862, 9862, 9862, 9862, 9862, 9862, 9862, 9893, 9893, 9893, 9893, 9893, 9893, 9893, 9893, 9893, 9893), class = "Date")), .Names = c("dates", "var1", "var2", "var3", "month"), row.names = c(NA, -27L), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

您可以使用库 dplyr 计算该聚合级别的均值和频率。

library(lubridate)
library(dplyr)

myFrame$month <- as.Date(cut(myFrame$dates, breaks = "month"))

myFrame_means <- myFrame %>%
                select(-dates) %>%
                group_by(month) %>%
                summarise_each(funs(mean))

myFrame_freq <- myFrame %>%
                    group_by(month) %>%
                    summarise(freq_v1 = sum(var1 !=0),
                              freq_v2 = sum(var2 !=0),
                              freq_v3 = sum(var3 !=0))

cbind(myFrame_means, myFrame_freq[2:4])

最后一个命令输出:

       month        var1       var2       var3 freq_v1 freq_v2 freq_v3
1 1997-01-01 -0.09049774 -0.1018100 -0.2036199       7       9       9
2 1997-02-01  0.15384615  0.1730769  0.3461538       5       5       5

答案 1 :(得分:0)

这可能是您正在寻找的吗?

library(dplyr)
x %>%
  group_by(month) %>%
  summarise_each(funs(mean, length), var1, var2, var3)

带输出:

Source: local data frame [2 x 7]

   month   var1_mean  var2_mean  var3_mean var1_length var2_length var3_length
  (date)       (dbl)      (dbl)      (dbl)       (int)       (int)       (int)
1 1997-01-01 -0.09049774 -0.1018100 -0.2036199          17          17          17
2 1997-02-01  0.15384615  0.1730769  0.3461538          10          10          10