使用 ids 计算数据帧中的共现次数

发布于 2025-01-14 20:04:32 字数 653 浏览 4 评论 0原文

我意识到有很多类似的问题,但它们都解决了略有不同的问题,我已经被困了一段时间。

我有一个包含 2 个变量的所有唯一组合的 dataframe ,如下所示:

df = data.frame(id = c('c1','c2','c3','c2','c3','c1','c3'),
                groupid = c('g1','g1','g1','g2','g2','g3','g3'))

我需要以下输出:

   c1 c2 c3
c1  3  1  2
c2  1  3  2
c3  2  2  3

换句话说,我需要计算每对客户 ID 在同一组中出现的频率。

似乎是一个基本问题,但我无法弄清楚。我尝试:

  1. 进行交叉连接以查找 (cid1,groupid,cid2) 的所有可能组合,
  2. 循环遍历所有组合,并检索与 cid1 匹配的唯一组以及与匹配 cid2
  3. 获取交集的长度

..但这将永远运行,所以我正在寻找一种高效且最好是干净的解决方案(使用tidyr/dplyr)。

I realize there are a lot of similar questions but they all tackle a slightly different problem and I have been stuck for a while.

I have a dataframe of all unique combinations of 2 variables as follows:

df = data.frame(id = c('c1','c2','c3','c2','c3','c1','c3'),
                groupid = c('g1','g1','g1','g2','g2','g3','g3'))

And I need the following output:

   c1 c2 c3
c1  3  1  2
c2  1  3  2
c3  2  2  3

In other words I need to count how often each pair of customer ids occur in the same group.

Seems like a basic question, but I can't figure it out. I tried:

  1. making a cross join to find all possible combinations of (cid1,groupid,cid2)
  2. looping through all of them and retrieving unique groups that match cid1 and unique groups that match cid2
  3. taking the length of the intersection

..but this would take forever to run, so I am looking for an efficient and preferably clean solution (using tidyr/dplyr).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2025-01-21 20:04:32

在通过table获取两列的频率计数后,我们可以使用crossprod

crossprod(table(df[2:1]))

We may use crossprod after getting the frequency count with table on the two columns

crossprod(table(df[2:1]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文