找出R中所有可能对的频率

时间:2014-08-10 14:03:53

标签: r plyr opendata

我使用R处理大量药物和反应数据集。目前,我的数据结构非常高,可以列出报告ID号,药物名称和报告的反应。正如您所知,ID与药物和药物与反应之间存在一对多的关系。

请记住,这个数据集比我在这里复制的数据要大得多,我想知道如何找到哪些药物导致什么反应和频率

最重要的是,我对如何处理这样的问题很感兴趣。数据结构是否正确?我应该阅读哪些概念或库?

以下是指向某些实际数据的链接:https://www.dropbox.com/s/kzx4mpyytbo9zil/query_result.csv

   ID    DRUG                                REACTION
1  1827  ASPIRIN                           CHEST PAIN
2  1827  CLARINEX                          CHEST PAIN
3  1827  ASPIRIN                                COUGH
4  1827  CLARINEX                               COUGH
5  1827  ASPIRIN                HAEMOGLOBIN DECREASED
6  1827  CLARINEX               HAEMOGLOBIN DECREASED
7  1827  ASPIRIN           NEUTROPHIL COUNT INCREASED
8  1827  CLARINEX          NEUTROPHIL COUNT INCREASED
9  1827  ASPIRIN               PHARYNGOLARYNGEAL PAIN
10 1827  CLARINEX              PHARYNGOLARYNGEAL PAIN
...

在我的小脑中,最终结果看起来像这样......

    Drug1       Drug2       Reaction            Frequency
1   tylenol     alcohol     hepatic failure     298
2   advil       aleve       bleeding            201 
3   aspirin     advil       renal failure       199
4   docusate    senna       diarrhea            146
5   senna       sudafed     palpitations        121
6   xanax       alcohol     sedation            111
7   clarinex    benadryl    dry mouth           96
...
569 ASPIRIN     CLARINEX    CHEST PAIN          2

Drug1和Drug2是整个数据集中频率最高的药物对。 A"药物对"被定义为具有相同报告ID的两种药物的任何组合。上面的示例输出将被解释为,"第1行有298个唯一的报告ID,其中肝功能衰竭就是反应。"

1 个答案:

答案 0 :(得分:2)

好的,我试着回答 - 我希望我能正确地回答这个问题。代码更倾向于给出一些想法,而不是优雅/最终 请注意:我故意用于循环而不是可能的矢量化/应用函数,以使其更容易理解(熟悉应用函数的人也将进行for循环;-))。
请注意2:由于我没有超过一小部分数据,我无法测试整个数据集的代码!
编辑:基于上述示例的列 - 可能与csv数据不同。

要点是:

  • unique[等。
  • utils::combn获取组合
  • 要计算的总和(FALSE / TRUE值)

希望有所帮助!

require(utils)

df <- read.table(header=TRUE, 
text="LINE ID DRUG REACTION
1 1827 ASPIRIN CHEST_PAIN
2 1827 CLARINEX CHEST_PAIN
3 1827 ASPIRIN COUGH
4 1827 CLARINEX COUGH
5 1827 ASPIRIN HAEMOGLOBIN_DECREASED
6 1827 CLARINEX HAEMOGLOBIN_DECREASED
7 1827 ASPIRIN NEUTROPHIL_COUNT_INCREASED
8 1827 CLARINEX NEUTROPHIL_COUNT_INCREASED
9 1827 ASPIRIN PHARYNGOLARYNGEAL_PAIN
10 1827 CLARINEX PHARYNGOLARYNGEAL_PAIN")

# temporary object to collect if a combination is present
Results <- data.frame(Drug1=NA, Drug2=NA, Reaction=NA, Reaction.occurs=NA)
n=1 # start first line in Results object

#  walk through each ID ... 
for (ID in unique(df$ID)) { 

  # ... and each possible pair of drugs within a (report) ID ...
  drug.pairs <- utils::combn(x=unique(df[df$ID == ID, "DRUG"]), m=2) # the columns 
  for (ii in 1:ncol(drug.pairs)) {

    # ... and each reaction ...
    for (reaction in unique(df$REACTION)) {
      Results[n, "Drug1"] <- drug.pairs[1,ii]
      Results[n, "Drug2"] <- drug.pairs[2,ii]
      Results[n, "Reaction"] <- reaction
      Results[n, "Reaction.occurs"] <- drug.pairs[1,ii] %in% df[df$REACTION == reaction & df$ID == ID, "DRUG"] &
        drug.pairs[2,ii] %in% df[df$REACTION == reaction & df$ID == ID, "DRUG"]
      n <- n+1
    }
  }
}

head(Results)

# then find the unique Drug1 - Drug2 -Reaction combinations, and count the TRUE values:
(Results[!duplicated(Results[,1:3]), ][,1:3])
(unique(Results[, 1:3]))

# Results2 contains only the unique combinations
Results2 <- Results[!duplicated(Results[,1:3]), ][,1:3]

# calculatethe frequencies
for (i in 1:nrow(Results2)) {
  Results2[i, "Frequency"] <- sum(Results[Results$Drug1 == Results2[i, "Drug1"] & 
                                            Results$Drug2 == Results2[i, "Drug2"] & 
                                            Results$Reaction == Results2[i, "Reaction"], ]$Reaction.occurs)
}

Results2
# --- end ----

给出:

    Drug1    Drug2                   Reaction Frequency
1 ASPIRIN CLARINEX                 CHEST_PAIN         1
2 ASPIRIN CLARINEX                      COUGH         1
3 ASPIRIN CLARINEX      HAEMOGLOBIN_DECREASED         1
4 ASPIRIN CLARINEX NEUTROPHIL_COUNT_INCREASED         1
5 ASPIRIN CLARINEX     PHARYNGOLARYNGEAL_PAIN         1