gpt4 book ai didi

R组由|计算按另一列分组的不同值

转载 作者:行者123 更新时间:2023-12-04 00:04:29 30 4
gpt4 key购买 nike

如何计算每个页面名称的不同访问 ID 的数量?

visit_id  post_pagename
1 A
1 B
1 C
1 D
2 A
2 A
3 A
3 B

结果应该是:
post_pagename distinct_visit_ids
A 3
B 2
C 1
D 1

试过
test_df<-data.frame(cbind(c(1,1,1,1,2,2,3,3),c("A","B","C","D","A","A","A","B")))
colnames(test_df)<-c("visit_id","post_pagename")
test_df

test_df %>%
group_by(post_pagename) %>%
summarize(vis_count = n_distinct(visit_id))

但这只给了我数据集中不同的visit_id数量

最佳答案

单程

test_df %>%
distinct() %>%
count(post_pagename)

# post_pagename n
# <fct> <int>
# 1 A 3
# 2 B 2
# 3 C 1
# 4 D 1

或其他
test_df %>%
group_by(post_pagename) %>%
summarise(distinct_visit_ids = n_distinct(visit_id))

# A tibble: 4 x 2
# post_pagename distinct_visit_ids
# <fct> <int>
#1 A 3
#2 B 2
#3 C 1
#4 D 1

*D has one visit, so it must be counted*

关于R组由|计算按另一列分组的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50953598/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com