我有以下数据:
CustomerNumber | LogEntry
1 2016-02-03
1 2016-02-05
1 2016-02-07
2 2016-02-05
2 2016-02-07
我希望它总结如下:
CustomerNumber | Frequency
1 3
2 2
我找到了解决它的方法,但它并不是非常优雅(我是新手)。
customer_log <- active_sessions
unique <- unique(customer_log$customer_number)
session_starts = character()
customer_number = character()
for (i in 1:length(unique)) {
session_starts[i] <- nrow(na.omit(customer_log[customer_log$customer_number == unique[i],]))
customer_number[i] <- unique[i]
}
active_sessions = data.frame(session_starts, customer_number)
有没有人建议如何更好地解决这个问题?非常感谢。
答案 0 :(得分:1)
您可以使用sqldf
包轻松实现此目的。
library(sqldf)
sqldf("SELECT CustomerNumber, count(*) AS Frequency FROM df GROUP BY CustomerNumber")