dplyr无法按月分组

时间:2016-11-03 05:39:27

标签: r dplyr

我想按月和按年分组,但结果显示" eval中的错误(expr,envir,enclos):object' MM'找不到"。有人可以帮忙吗?谢谢!

library(dplyr)
uq_app %>% 
select(Date, sales, convs) %>%
mutate(MM= format(Date, "%m"), YY= format(Date, "%Y")) %>%
group_by(MM, YY) %>%
summarise(total_conv = sum(convs))

Date           sl convs fi op  cl
03/10/2015      0   0    0  0   0
03/10/2015      0   0   36  13  7
03/10/2015      0   0   13  6   1
03/10/2015      0   0   17  6   1
03/10/2015      0   0   1   1   0
03/10/2015      0   0   30  19  5
03/10/2015  272.6   2   13  3   2
04/10/2015      0   0   0   0   0
04/10/2015  512.2   6   56  27  7
04/10/2015  112.4   2   21  8   1
04/10/2015  142.2   1   24  11  0
04/10/2015      0   0   4   0   0
04/10/2015   60.4   1   25  11  0

1 个答案:

答案 0 :(得分:1)

我们需要在提取“月份”之前转换为Date课程。和'年'

uq_app %>% 
    mutate(Date = as.Date(Date, '%m/%d/%Y')) %>%
    group_by(MM = format(Date, '%m'), YY = format(Date, '%Y')) %>% 
    summarise(total_conv = sum(convs))
#    MM    YY total_conv
#  <chr> <chr>      <int>
#1    03  2015          2
#2    04  2015         10