英文:
Count number of unique activities
问题
以下是您要翻译的内容:
我有一个数据框(简化版本),如下所示:
df <- data.frame(ID = rep('A',7), Activity =
c('Login','Login','cat1','Login','cat2','Login','Login'))
这是我想要做的:
- 从第一行开始
- 初始化
session=0
- 创建一个数据框来保存
path
和count
- 如果
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 <- data.frame(ID = rep('A',7), Activity =
c('Login','Login','cat1','Login','cat2','Login','Login'))
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
andcount
-
If the
Activity
is equal toLogin
, thensession=1
, check the next row'sActivity
and record it. This will be apath
until the nextLogin
-
continue until you hit the next
Login
, then setsession=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 == "Login")), function(i){
paste(i, collapse = ",")
})))
# 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 %>%
group_by(grp = cumsum(Activity == "Login")) %>%
summarise(Path = toString(Activity)) %>%
count(Path, name = "count")
-output
# A tibble: 3 × 2
Path count
<chr> <int>
1 Login 3
2 Login, cat1 1
3 Login, cat2 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论