gpt4 book ai didi

r - 如何根据r中的常见项目找到桶重叠

转载 作者:行者123 更新时间:2023-12-03 23:11:25 26 4
gpt4 key购买 nike

我有这样的数据,其中存储桶可以有不同数量的项目:

Bucket A | Item 1
Bucket A | Item 2
Bucket A | Item 3
Bucket B | Item 3
Bucket B | Item 4
Bucket C | Item 1
Bucket C | Item 5
Bucket C | Item 2

我想找到所有桶的项目重叠,所以我用以下格式得到它(左边是基本桶):
         Bucket A | Bucket B | Bucket C
Bucket A 100% | 33% | 66%
Bucket B 50% | 100% | 0%
Bucket C 66% | 0% | 100%

最佳答案

这是一种使用 dplyr 的方法:

temp <- df %>%
group_by(V2) %>%
do(expand.grid(.$V1, .$V1, stringsAsFactors=FALSE)) %>%
ungroup() %>%
select(Var1, Var2) %>%
table()
temp / diag(temp)

Var2
Var1 Bucket A Bucket B Bucket C
Bucket A 1.0000000 0.3333333 0.6666667
Bucket B 0.5000000 1.0000000 0.0000000
Bucket C 0.6666667 0.0000000 1.0000000

数据
df <- structure(list(V1 = c("Bucket A ", "Bucket A ", "Bucket A ", 
"Bucket B ", "Bucket B ", "Bucket C ", "Bucket C ", "Bucket C "
), V2 = c(" Item 1", " Item 2", " Item 3", " Item 3", " Item 4",
" Item 1", " Item 5", " Item 2")), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA,
-8L))

关于r - 如何根据r中的常见项目找到桶重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42822622/

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