麻烦按日期分组数据

时间:2019-04-09 18:00:53

标签: r time-series

尝试按月和年对数据进行分组,但始终收到此错误消息:

  

对象[[名称,精确= TRUE]]中的错误:类型为“关闭”的对象为   不可子集化

这是我的代码:

passflow %>%
  group_by(txdate=floor_date(date,"month"))%>%
  summarize(flows=sum(flows))

这是我的数据集:

 weekday     txdate flows month year
1       1 2012-01-02 24317     1 2012
2       2 2012-01-03 20116     1 2012
3       3 2012-01-04 11344     1 2012
4       4 2012-01-05 11621     1 2012
5       5 2012-01-06 18203     1 2012
6       6 2012-01-07 20430     1 2012

1 个答案:

答案 0 :(得分:0)

group_by提取format中的年份和月份。这样做的好处是可以对输出df中的格式进行更多控制(如果需要)。

passflow %>%
  group_by(year = format(passflow$txdate, "%Y"), # group by year
           month = format(passflow$txdate, "%B")) %>% # group by month
  summarize(flows = sum(flows))

# A tibble: 2 x 3
# Groups:   year [1]
  year  month    flows
  <chr> <chr>    <int>
1 2012  February 38633
2 2012  January  67398

(我将一些数据移至2月进行检查)

正如divibisan指出的那样,您在操作中有一个错误:

passflow %>%
  group_by(txdate=floor_date(txdate,"month"))%>%
  summarize(flows=sum(flows))
相关问题