统计唯一活动的数量

huangapple go评论52阅读模式
英文:

Count number of unique activities

问题

以下是您要翻译的内容:

我有一个数据框(简化版本),如下所示:

df <- data.frame(ID = rep('A',7), Activity = 
 c('Login','Login','cat1','Login','cat2','Login','Login'))

这是我想要做的:

  • 从第一行开始
  • 初始化 session=0
  • 创建一个数据框来保存 pathcount
  • 如果 Activity 等于 Login,那么 session=1,检查下一行的 Activity 并记录它。这将成为一个 path,直到下一个 Login
  • 继续直到遇到下一个 Login,然后设置 session=2
  • 这个示例的最终结果将是:
         Path          count
         Login           3
         Login, cat1     1
         Login, cat2     1
英文:

I have a dataframe (simplified version) as follows:

df &lt;- data.frame(ID = rep(&#39;A&#39;,7), Activity = 
 c(&#39;Login&#39;,&#39;Login&#39;,&#39;cat1&#39;,&#39;Login&#39;,&#39;cat2&#39;,&#39;Login&#39;,&#39;Login&#39;))

  ID Activity
1  A    Login
2  A    Login
3  A     cat1
4  A    Login
5  A     cat2
6  A    Login
7  A    Login

Here is what I wish to do:

  • start from the first row

  • initiate session=0

  • create a dataframe to hold path and count

  • If the Activity is equal to Login, then session=1, check the next row's Activity and record it. This will be a path until the next Login

  • continue until you hit the next Login, then set session=2.

  • The final outcome for this example would be:

           Path          count
           Login           3
           Login, cat1     1
           Login, cat12    1
    

答案1

得分: 0

创建基于 "Login" 的组,分割,然后在每个组中粘贴,最后使用表格进行聚合:

data.frame(
  table(
    sapply(split(df$Activity, cumsum(df$Activity == "Login")), function(i){
      paste(i, collapse = ",")
    })))
#         Var1 Freq
# 1      Login    3
# 2 Login,cat1    1
# 3 Login,cat2    1
英文:

Create groups based on "Login", split, then paste per group, finally, aggregate using table:

data.frame(
  table(
    sapply(split(df$Activity, cumsum(df$Activity == &quot;Login&quot;)), function(i){
      paste(i, collapse = &quot;,&quot;)
    })))
#         Var1 Freq
# 1      Login    3
# 2 Login,cat1    1
# 3 Login,cat2    1

答案2

得分: 0

使用dplyr库进行数据处理的示例代码如下:

library(dplyr)
df %>%
 group_by(grp = cumsum(Activity == "Login")) %>%
 summarise(Path = toString(Activity)) %>%
 count(Path, name = "count")

输出结果如下:

# A tibble: 3 × 2
  Path        count
  <chr>       <int>
1 Login       3
2 Login, cat1 1
3 Login, cat2 1

请注意,我只翻译了代码和注释部分,不包括输出结果。

英文:

Using dplyr

library(dplyr)
df %&gt;% 
 group_by(grp = cumsum(Activity == &quot;Login&quot;)) %&gt;%
 summarise(Path = toString(Activity)) %&gt;% 
 count(Path, name = &quot;count&quot;)

-output

# A tibble: 3 &#215; 2
  Path        count
  &lt;chr&gt;       &lt;int&gt;
1 Login           3
2 Login, cat1     1
3 Login, cat2     1

huangapple
  • 本文由 发表于 2023年4月19日 18:48:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定