2017-07-19 251 views
-1

我正在使用下面的代码按月分组来计算总和或计数。但是,SLARespond列似乎是整个数据集的总和,而不是每个月。如何按月累加或计数R?

任何我能解决问题的方法? 也可以,而不是总和功能,我可以做计数功能与SLAIncident $ IsSlaRespondByViolated == 1

欣赏的帮助!

SLAIncident <- SLAIncident %>% 
mutate(month = format(SLAIncident$CreatedDateLocal, "%m"), year = format(SLAIncident$CreatedDateLocal, "%Y")) %>% 
group_by(year, month) %>% 
summarise(SLARespond = sum(SLAIncident$IsSlaRespondByViolated)) 
+2

不要使用'SLAIncident $ CreatedDateLocal',而不是在所有变量中使用'CreatedDateLocal'和类似的。 – lmo

+0

并将'SLAIncident $ IsSlaRespondByViolated'改为'IsSlaRespondByViolated'。 –

回答

0

如果您可以提供一小部分数据集来说明您的示例,那就太棒了。我首先要确保你的月/年是角色或因素,以便dplyr能抓住他们。用总和包装的ifelse函数也应该符合问题第二部分的标准。我使用你的代码在这里的日期,月份和年份转换,但我建议lubridate

SLAIncident <- SLAIncident %>% 
    mutate(month = as.character(format(SLAIncident$CreatedDateLocal, "%m")), 
     year = as.character(format(SLAIncident$CreatedDateLocal, "%Y"))) %>% 
    group_by(year, month) %>% 
    summarise(SLARespond = sum(IsSlaRespondByViolated), 
      sla_1 = sum(ifelse(isSlaRespondByViolated == 1, 1, 0))) 

另外,作为在评论中暗示,这些列名很长,可能使用一些整理