在 R 中使用多列求和出现次数

Sum occurrences using multiple columns in R

我正在尝试查看 table 个在特定日期收到特定错误消息的帐户总数的结果。但是,一个帐户在任何一天都可能收到数百条不同的错误消息。例如,这个 table 被称为 ERROR 而我的列是:

     date        error     acct#     freq  
2016-04-20       panda       2        1
2016-04-20         pig       2        1
2016-04-20       panda       3        1
2016-04-20       panda       7        1
2016-04-25       panda       4        1
2016-04-27        bird       4        1
2016-04-27        bird       3        1

这是我的代码:

Error_Freq = ddply(ERROR, .(date), summarize, freq=sum(freq))
View(Error_Freq)

但这只给我每天的帐户总数。 我希望输出看起来像:

     date        error     freq 
2016-04-20       panda       3
2016-04-20         pig       1
2016-04-25       panda       1
2016-04-27        bird       2 

我应该用另一种方式来看待这个吗?

我们还需要使用 error 作为分组变量

library(dplyr) 
df1 %>% 
   group_by(date, error) %>% 
   summarise(freq = sum(freq))
     date error  freq
#        <chr> <chr> <int>
#1 2016-04-20 panda     3
#2 2016-04-20   pig     1
#3 2016-04-25 panda     1
#4 2016-04-27  bird     2

或使用ddply

library(plyr)
ddply(df1, .(date, error), summarize, freq=sum(freq))