r - Count frequency of events in R
问题描述
Within a data frame (see below), I would like to compute the frequency of each activity/event (see second column). The frequency of the events should be shown in the appropriate columns (third until sixth).
E.g. in column 3 (Open Document) and row 2 (user 1) should be written the number "1" and in column 6 (Close Document) should also be computed "1".
Or if user 4 edits a document five times, then "5" should be shown in column 5 (Edit Document) and row 5 (user 4).
Also a loop-function is needed due to a massive amount of users.
user activity Open Document Read Document Edit Document Close Document
1 c("Open Document", "Close Document", …)
2 c("Open Document", "Read Document", …)
3 c("Open Document", "Close Document", …)
4 c("Open Document", "Edit Document", …)
Thanks for your help.
解决方案
你可以用它table
来计算事件的频率。我使用strsplit
拆分为单个活动并factor
与所有活动的级别一起制作:
x[,-1:-2] <- do.call(rbind, lapply(strsplit(x$activity, ",")
, function(i) table(factor(i, levels=colnames(x)[-1:-2]))))
x
# user activity Open Read Edit Close
#1 1 Open,Close 1 0 0 1
#2 2 Open,Read 1 1 0 0
#3 3 Open,Close 1 0 0 1
#4 4 Open,Edit 1 0 1 0
数据:
x <- data.frame(user=1:4, activity=c("Open,Close", "Open,Read", "Open,Close", "Open,Edit")
, Open=0, Read=0, Edit=0, Close=0, stringsAsFactors=FALSE)
推荐阅读
- php - 数次加起来超过24小时
- reactjs - 为什么有些开发者在 React.js 的类中使用构造函数和 super?
- python - 如何将变量从 python 发送到 shell,然后发送到 hive?
- opengl - 在片段着色器中绘制别名像素完美线?
- .net - 是否可以在 Windows Sandbox 上安装 .NET 应用程序?
- python - 对于“str.contains()”,如何根据列表而不是一个字符串进行过滤
- html - 如何取消转义 HTML 元标记中的特殊字符?
- java - 从 R 调用 Java 类时的问题
- flutter - 如何修复错误以使用 Firestore 填充下拉按钮
- powershell - Terraform,从powershell gridview中选择值