Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中按月分组求和或计数?_R - Fatal编程技术网

如何在R中按月分组求和或计数?

如何在R中按月分组求和或计数?,r,R,我使用下面的代码按月分组求和或计数。然而,SLARespond列似乎是整个数据集的总和,而不是每个月的总和 有什么办法可以解决这个问题吗? 另外,我可以用slaivent$isslarespondbyinvalized==1来代替sum函数来执行count函数吗 感谢你的帮助 SLAIncident <- SLAIncident %>% mutate(month = format(SLAIncident$CreatedDateLocal, "%m"), year = format(S

我使用下面的代码按月分组求和或计数。然而,SLARespond列似乎是整个数据集的总和,而不是每个月的总和

有什么办法可以解决这个问题吗? 另外,我可以用slaivent$isslarespondbyinvalized==1来代替sum函数来执行count函数吗

感谢你的帮助

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