r循环迭代并找到每个项目之间的独特组合

发布于 2025-01-29 10:09:30 字数 1667 浏览 2 评论 0原文

    concept_id                                  concept_name  event
1:     443387                    Malignant tumor of stomach comorb
2:    4193704 Type 2 diabetes mellitus without complication comorb
3:    4095320            Malignant tumor of body of stomach comorb
4:     201826                      Type 2 diabetes mellitus comorb
5:    4174977          Retinopathy due to diabetes mellitus comorb

对于上述数据,我正在尝试创建概念_ID组合的列表。有5个概念ID,因此当我们用另一个概念_ID迭代每个概念_ID时,我们会得到这样的列表。

nrow(comorb_event)
for (i in (1:nrow(comorb_event))) {
  for (j in (1:nrow(comorb_event))){
    print(paste(i,j))
  }
}

[1] "1 1"
[1] "1 2"
[1] "1 3"
[1] "1 4"
[1] "1 5"
[1] "2 1"
[1] "2 2"
[1] "2 3"
[1] "2 4"
[1] "2 5"
[1] "3 1"
[1] "3 2"
[1] "3 3"
[1] "3 4"
[1] "3 5"
[1] "4 1"
[1] "4 2"
[1] "4 3"
[1] "4 4"
[1] "4 5"
[1] "5 1"
[1] "5 2"
[1] "5 3"
[1] "5 4"
[1] "5 5"

我的输出不是我期望的。由于项目[1,1]是相同的项目,我们可以避免这种情况,并且[1,2]类似地,我们也可以删除该项目[2,1]。删除冗余组合后,预期列表将是这样的:

[1] "1 2"
[1] "1 3"
[1] "1 4"
[1] "1 5"
[1] "2 3"
[1] "2 4"
[1] "2 5"
[1] "3 4"
[1] "3 5"
[1] "4 5"

示例数据

structure(list(concept_id = c("443387", "4193704", "4095320", 
"201826", "4174977"), concept_name = c("Malignant tumor of stomach", 
"Type 2 diabetes mellitus without complication", "Malignant tumor of body of stomach", 
"Type 2 diabetes mellitus", "Retinopathy due to diabetes mellitus"
), event = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("comorb", 
"drug", "primary_dx"), class = "factor")), class = c("data.table", 
"data.frame"), row.names = c(NA, -5L), .internal.selfref = <pointer: 0x5642431689a0>)
    concept_id                                  concept_name  event
1:     443387                    Malignant tumor of stomach comorb
2:    4193704 Type 2 diabetes mellitus without complication comorb
3:    4095320            Malignant tumor of body of stomach comorb
4:     201826                      Type 2 diabetes mellitus comorb
5:    4174977          Retinopathy due to diabetes mellitus comorb

For the above data, I am trying to create a list of combinations for concept_ids. There are 5 concept ids so when we iterate each concept_id with another concept_id we get a list something like this.

nrow(comorb_event)
for (i in (1:nrow(comorb_event))) {
  for (j in (1:nrow(comorb_event))){
    print(paste(i,j))
  }
}

[1] "1 1"
[1] "1 2"
[1] "1 3"
[1] "1 4"
[1] "1 5"
[1] "2 1"
[1] "2 2"
[1] "2 3"
[1] "2 4"
[1] "2 5"
[1] "3 1"
[1] "3 2"
[1] "3 3"
[1] "3 4"
[1] "3 5"
[1] "4 1"
[1] "4 2"
[1] "4 3"
[1] "4 4"
[1] "4 5"
[1] "5 1"
[1] "5 2"
[1] "5 3"
[1] "5 4"
[1] "5 5"

My output is not what I expect. Since item [1,1] are same items we can avoid that, and similarly item [2,1] is already covered by [1,2] we can remove that too. The expected list would be something like this after removing the redundant combinations:

[1] "1 2"
[1] "1 3"
[1] "1 4"
[1] "1 5"
[1] "2 3"
[1] "2 4"
[1] "2 5"
[1] "3 4"
[1] "3 5"
[1] "4 5"

Sample data

structure(list(concept_id = c("443387", "4193704", "4095320", 
"201826", "4174977"), concept_name = c("Malignant tumor of stomach", 
"Type 2 diabetes mellitus without complication", "Malignant tumor of body of stomach", 
"Type 2 diabetes mellitus", "Retinopathy due to diabetes mellitus"
), event = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("comorb", 
"drug", "primary_dx"), class = "factor")), class = c("data.table", 
"data.frame"), row.names = c(NA, -5L), .internal.selfref = <pointer: 0x5642431689a0>)

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

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

发布评论

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

评论(1

失退 2025-02-05 10:09:30

我们需要combn

t(combn(seq_len(nrow(comorb_event)), 2))

We need combn

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