首页 > 解决方案 > 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.

标签: rdataframecountfrequency

解决方案


你可以用它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)

推荐阅读