簡體   English   中英

尋找R中所有可能對的頻率

[英]Finding frequencies of all possible pairs in R

我正在使用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是整個數據集中頻率最高的葯物對。 “葯物對”定義為具有相同報告ID的兩種葯物的任意組合。 上面的示例輸出將被解釋為“第1行具有298個唯一的報告ID,其反應是肝衰竭。”

好的,我嘗試一個答案-希望我能正確回答問題。 該代碼旨在提供一些想法,而不是優雅/最終的。
請注意:我故意使用for循環,而不是使用可能的向量化/ apply函數,以使其更易於理解(熟悉apply函數的人也會理解for循環;-)。
請注意2:由於我沒有太多的數據,因此無法測試整個數據集的代碼!
編輯 :基於以上示例的列-可能與csv數據不同。

關鍵點是:

  • unique [
  • utils::combn獲取組合
  • sum(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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM