2017-07-19 107 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))) 

另外,作爲在評論中暗示,這些列名很長,可能使用一些整理