如何在R中按月分组求和或计数?
我使用下面的代码按月分组求和或计数。然而,SLARespond列似乎是整个数据集的总和,而不是每个月的总和 有什么办法可以解决这个问题吗? 另外,我可以用slaivent$isslarespondbyinvalized==1来代替sum函数来执行count函数吗 感谢你的帮助如何在R中按月分组求和或计数?,r,R,我使用下面的代码按月分组求和或计数。然而,SLARespond列似乎是整个数据集的总和,而不是每个月的总和 有什么办法可以解决这个问题吗? 另外,我可以用slaivent$isslarespondbyinvalized==1来代替sum函数来执行count函数吗 感谢你的帮助 SLAIncident <- SLAIncident %>% mutate(month = format(SLAIncident$CreatedDateLocal, "%m"), year = format(S
SLAIncident <- SLAIncident %>%
mutate(month = format(SLAIncident$CreatedDateLocal, "%m"), year = format(SLAIncident$CreatedDateLocal, "%Y")) %>%
group_by(year, month) %>%
summarise(SLARespond = sum(SLAIncident$IsSlaRespondByViolated))
sla事件%
变化(月=格式(slaivent$createdDataTelocal,“%m”)、年=格式(slaivent$createdDataTelocal,“%Y”))%>%
分组单位(年、月)%>%
总结(SLARespond=sum(SLAIncident$IsslarRespondby违反))
如果您能提供一小部分数据集来说明您的示例,那将非常好。我会首先确保你的月份/年份是字符或因子,以便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)))
sla事件%
mutate(month=as.character(格式为($createdDataTelocal,“%m”)),
year=as.character(格式为($createdDataTelocal,“%Y”))%>%
分组单位(年、月)%>%
总结(SLARespond=sum(IsSlaRespondByVillected),
sla_1=sum(ifelse(isslarespondbyinvalized==1,1,0)))
同样正如评论中所暗示的,这些列名非常长,可能需要一些整理不要使用
slaivent$createdDataTelocal
,而是在所有变量中使用createdDataTelocal
和类似的名称。并且将slaivent$IsslarespondbyIncreated)
更改为IsslarespondbyIncreated
。