R loop to iterate and find unique combination between each item

Viewed 25
    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>)
1 Answers

We need combn

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