簡體   English   中英

R:從一個數據框中提取行,基於列名匹配另一個數據框中的值

[英]R: Extract Rows from One Data Frame, Based on Column Names Matching Values from Another Data Frame

我想知道如何基於數據幀A中的某些列名提取數據幀(數據幀A)的一列中的值,該數據幀A包含來自另一個數據幀(數據幀B)的多個列的值。

進一步來說。 我有兩個數據框:

數據框A包含出生缺陷的組合。 每行是不同的組合,每列是該組合中包含的缺陷的編號。

# Combinations data frame 
combos <- data.frame("combo_no"=c(1:4),
                     "Defect_A" = c(1,1,1,1),
                     "Defect_B" = c(3,2,3,4),
                     "Defect_C" = c(4,4,NA,7),
                     "Defect_D" = c(5,5,NA,8),
                     "Defect_E" = c(6,6,NA,NA))

數據框B包含個案。 第一列具有唯一標識符(CASE_ID)。 其余的列是特定出生缺陷的數量,“出生缺陷存在”為“1”,“不存在”為“0”。

# Cases data set 
set.seed(99)
CASE_ID = c(1001:1005)
case1 = sample(0:1, 10, replace=TRUE)  
case2 = sample(0:1, 10, replace=TRUE)  
case3 = sample(0:1, 10, replace=TRUE)  
case4 = sample(0:1, 10, replace=TRUE)  
case5 = sample(0:1, 10, replace=TRUE)  
def<-data.frame(rbind(case1, case2, case3, case4, case5))
colnames(def)<- c(1:10)
cases<-cbind(CASE_ID,def)

期望的輸出:我想從數據框A獲得CASE_ID的列表,其具有來自數據框B的出生缺陷的組合。我還想指定存在哪個組合。 理想情況下,輸出結果如下:

# Desired Output
output <- data.frame("CASE_ID" = c(1002,1003),
                     "combo_no" = c(3,1))

謝謝您的幫助。

這里的解決方案,長期以來一步一步地評論它:

### my random generated cases DF:
cases
      CASE_ID 1 2 3 4 5 6 7 8 9 10
case1    1001 1 0 1 1 1 1 1 0 0  0
case2    1002 1 1 0 1 1 1 0 0 0  0
case3    1003 0 0 1 1 1 0 0 1 0  0
case4    1004 1 0 0 1 0 0 1 1 1  1
case5    1005 1 0 1 1 0 1 0 0 1  0
### initialize vectors to store found results
found_combos <- vector(); found_patients <- vector();
### open loop on combos rows
for (i in 1:nrow(combos)) {
  ### open empty vector to fill with the numbers that compose the defect
  defect_numbers <- vector()
  ### open loop on column and take the numbers
  for (col in colnames(combos)[2:length(colnames(combos))]) {
    number <- combos[i, col]
    if ( !is.na(number) ) defect_numbers <- append(defect_numbers, number)
  }
  ### sort the vector to avoid mismatch based on order
  defect_numbers <- sort( defect_numbers )
  ### open loop on patients table
  for ( pz in 1:nrow(cases) ) {
    pz_numbers <- sort( which( cases[pz,] == 1 )-1 )
    ### first condition: same length
    if ( length(pz_numbers) == length(defect_numbers) ) {
      ### same condition: exacly same numbers
      if (all(pz_numbers == defect_numbers)) {
        ### append to found results vectors
        found_patients <- append( found_patients, cases[pz,1] )
        found_combos <- append( found_combos, i )
      }
    }
  }
}

output <- data.frame("CASE_ID" = found_patients,
                     "combo_no" = found_combos)

### result:
output
  CASE_ID combo_no
1    1002        2

根據您的評論編輯:

只需將條件從等於%改為%:

### initialize vectors to store found results
found_combos <- vector(); found_patients <- vector();
for (i in 1:nrow(combos)) {
  ### open empty vector to fill with the numbers that compose the defect
  defect_numbers <- vector()
  ### open loop on column and take the numbers
  for (col in colnames(combos)[2:length(colnames(combos))]) {
    number <- combos[i, col]
    if ( !is.na(number) ) defect_numbers <- append(defect_numbers, number)
  }
  ### sort the vector to avoid mismatch based on order
  defect_numbers <- sort( defect_numbers )
  ### open loop on patients table
  for ( pz in 1:nrow(cases) ) {
    pz_numbers <- sort( which( cases[pz,] == 1 )-1 )
    ### only condition: all defect_numbers in combo_numbers vector
    if (all(defect_numbers %in% pz_numbers)) {
      ### append to found results vectors
      found_patients <- append( found_patients, cases[pz,1] )
      found_combos <- append( found_combos, i )
    }
  }
}

output <- data.frame("CASE_ID" = found_patients,
                     "combo_no" = found_combos)

暫無
暫無

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

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